C Program
#include <stdio.h> #include <math.h> int main() { double a, b, c, d, root1, root2; printf("Enter coefficients a, b and c: "); scanf("%lf %lf %lf", &a, &b, &c); d = b*b - 4*a*c; if (d > 0) { root1 = (-b + sqrt(d)) / (2*a); root2 = (-b - sqrt(d)) / (2*a); printf("Real and distinct roots: %.2lf and %.2lf", root1, root2); } else if (d == 0) { root1 = root2 = -b / (2*a); printf("Real and equal roots: %.2lf and %.2lf", root1, root2); } else { double real = -b / (2*a); double imag = sqrt(-d) / (2*a); printf("Complex roots: %.2lf+%.2lfi and %.2lf-%.2lfi", real, imag, real, imag); } return 0; }
C Output
Input: Enter coefficients a, b and c: 1 -3 2 Output: Real and distinct roots: 2.00 and 1.00
C++ Program
#include <iostream> #include <cmath> using namespace std; int main() { double a, b, c, d; cout << "Enter a, b and c: "; cin >> a >> b >> c; d = b*b - 4*a*c; if (d > 0) { cout << "Real and distinct roots: " << (-b + sqrt(d)) / (2*a) << " and " << (-b - sqrt(d)) / (2*a); } else if (d == 0) { cout << "Real and equal roots: " << -b / (2*a) << " and " << -b / (2*a); } else { double real = -b / (2*a), imag = sqrt(-d) / (2*a); cout << "Complex roots: " << real << "+" << imag << "i and " << real << "-" << imag << "i"; } }
C++ Output
Input: Enter a, b and c: 1 2 1 Output: Real and equal roots: -1 and -1
JAVA Program
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a, b and c: "); double a = sc.nextDouble(), b = sc.nextDouble(), c = sc.nextDouble(); double d = b*b - 4*a*c; if (d > 0) { System.out.println("Real and distinct roots: " + (-b + Math.sqrt(d)) / (2*a) + " and " + (-b - Math.sqrt(d)) / (2*a)); } else if (d == 0) { System.out.println("Real and equal roots: " + -b / (2*a) + " and " + -b / (2*a)); } else { double real = -b / (2*a), imag = Math.sqrt(-d) / (2*a); System.out.println("Complex roots: " + real + "+" + imag + "i and " + real + "-" + imag + "i"); } sc.close(); } }
JAVA Output
Input: Enter a, b and c: 1 4 5 Output: Complex roots: -2.0+1.0i and -2.0-1.0i
Python Program
import cmath a, b, c = map(float, input("Enter a, b and c: ").split()) d = b**2 - 4*a*c root1 = (-b + cmath.sqrt(d)) / (2*a) root2 = (-b - cmath.sqrt(d)) / (2*a) print(f"Roots are: {root1} and {root2}")
Python Output
Input: Enter a, b and c: 1 -5 6 Output: Roots are: (3+0j) and (2+0j)
In-Depth Explanation
Example
If you have the quadratic equation , the coefficients are a = 1, b = -3, and c = 2. Using the quadratic formula , we calculate the discriminant , which is greater than zero, meaning there are two distinct real roots: 2 and 1.
Real-Life Analogy
Imagine you’re building a roller coaster track and the quadratic equation represents the shape of the track. The roots are the exact points where the track touches the ground. If the discriminant is positive, the coaster touches the ground at two distinct points. If it’s zero, it touches the ground at exactly one point (a perfect bump). If it's negative, the track never touches ground at all — the roots are "imaginary" like a loop in the air.
Why It Matters
The idea of solving the roots of a quadratic equation is not only theoretical; it's ubiquitous — from physics homework on projectile motion, to economics for determining breakeven, to computer graphics for collision detection. Understanding how to work through various cases of discriminants in programming develops problem-solving abilities for branching logic and mathematical modeling.
Learning Insights
By coding quadratic roots, newbies get to practice decision making (if-else), working with floating-point numbers, and with mathematical libraries such as math in C/C++ and Python or Math in Java. You also learn complex numbers programming, which is important in simulations and high-end calculations.
SEO-Optimized Conclusion
Learning the process of calculating the roots of a quadratic equation in C, C++, Java, and Python is a fundamental coding practice for students and interview practice. It integrates mathematical reasoning with programming ability, demonstrating how real and complex roots are treated under various conditions. Whether working on physics problems, financial computations, or graphical simulations, being proficient in quadratic root calculation enables you to develop correct, efficient programs. This line-by-line process with code in several languages is more accessible for newcomers to comprehend and practice from, setting them up for academic exams as well as technical interviews.
Social Plugin