C program to find roots of quadratic equation
In this post, you will learn how to find the roots of quadratic equation using C programming language.
A quadratic equation in math is a second-degree equation of the given form -
ax² + bx + c = 0
Here a, b, are the coefficients, c is the constant term, and x is the variable. Since the variable x is of the second degree, there are two roots or answer for this quadratic equation.
The universal rule of quadratic equation defines that the value of 'a' cannot be zero, and the value of x is used to find the roots of the quadratic equation (a, b).
Roots of quadratic equation
The discriminant of a quadratic equation is calculated as b2 - 4ac. The nature of the roots depends on the Discriminant (D),
- If D > 0, the roots are real and distinct (unequal)
- If D = 0, the roots are real and equal.
- If D < 0, the roots are real and imaginary.
The given table represents the nature of the quadratic equation roots -
If discriminant(D) > 0 | |
If discriminant = 0 | |
If discriminant < 0 |
Algorithm of Roots of quadratic equation
1. Start
2. Input the coefficient variable, a, b and c.
3. D <- sqrt (b * b - 4 * a * c).
4. R1 <- (-b + D) / ( 2 * a).
5. R2 <- (-b - D) / (2 * a).
6. Print the roots R1 and R2.
7. Stop
C Program to Find Roots of a Quadratic Equation
#include<stdio.h>
# include<math.h>
int main(){
float a,b,c,root1,root2,d;
printf ("Enter the values of a b c: ");
scanf ("%f %f %f", &a, &b, &c);
//discriminant
d = b*b - 4*a*c;
// condition for real and different roots
if (d>0){
root1 = -b+sqrt (d) / (2*a);
root2 = -b-sqrt (d) / (2*a);
printf ("The real roots = %f %f", root1, root2);
}
// condition for real and equal roots
else if (d==0){
root1 = -b/(2*a);
root2 = -b/(2*a);
printf ("roots are equal = %f %f", root1, root2);
}
else
printf("Roots are imaginary.");
}
Output of the above code:
Enter the values of a b c: 3 5 2
The real roots = -4.833333 -5.166667
Enter the values of a b c: 3.2 4.2 5.2
Roots are imaginary.
Enter the values of a b c: 3 -2 -6
The real roots = 3.452966 0.547034
C Program to Find Roots of a Quadratic Equation using function
/* C program to find roots of a quadratic equation */
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
// Getting roots of quadratic equation
void RootsOfQuadEqu(int a, int b, int c)
{
// If a is 0, then equation is not quadratic
if (a == 0) {
printf("Invalid");
return;
}
int d = b * b - 4 * a * c;
double quad_sqrt = sqrt(abs(d));
if(d > 0) {
printf("Roots are real and different. \n");
printf("%f\n%f", (double)(-b + quad_sqrt) / (2 * a),
(double)(-b - quad_sqrt) / (2 * a));
}
else if(d == 0) {
printf("Roots are real and same. \n");
printf("%f", -(double)b / (2 * a));
}
else
{
printf("Roots are complex. \n");
printf("%f + i%f\n%f - i%f", -(double)b / (2 * a),
quad_sqrt/(2 * a), -(double)b / (2 * a), quad_sqrt/(2 * a));
}
}
// Driver code
int main()
{
int a = 4, b = -8, c = 15;
// Calling function
RootsOfQuadEqu(a, b, c);
return 0;
}
Output of the above code:
Roots are complex.
1.000000 + i1.658312
1.000000 - i1.658312
C program to find the roots of a quadratic equation using switch case
#include <stdio.h>
#include <math.h>
int main()
{
float a= 4.2, b=6.3, c=2.3;
float root1, root2, imaginary, d;
d = (b * b) - (4 * a *c);
switch(d > 0)
{
case 1:
root1 = (-b + sqrt(d) / (2 * a));
root2 = (-b - sqrt(d) / (2 * a));
printf("\n Roots are real and different: root1 = %.2f and root2 = %.2f", root1, root2);
break;
case 0:
switch(d < 0)
{
case 1: //True
root1 = root2 = -b / (2 * a);
imaginary = sqrt(-d) / (2 * a);
printf("\n Roots are complex: root1 = %.2f+%.2f and root2 = %.2f-%.2f", root1, imaginary, root2, imaginary);
break;
case 0: // False (Discriminant = 0)
root1 = root2 = -b / (2 * a);
printf("\n Roots are real and same: root1 = %.2f and root2 = %.2f", root1, root2);
break;
}
}
return 0;
}
Output of the above code:
Roots are real and different: root1 = -6.18 and root2 = -6.42
Related Articles
Prime factors of a number in cArmstrong number program in c
Write a program to check leap year in c
C program to find area of rectangle
C program to convert celsius to fahrenheit
Fibonacci series program in C using recursion
Write a program to find area of circle in C
C program to find greatest of three numbers
C program for addition of two numbers
C program to calculate compound interest
C program to find the ASCII value of a character
C program to convert Decimal to Octal
C program to convert decimal to binary
Write a C program to calculate Simple Interest
C program to check whether a number is even or odd
C program to reverse a number
C program to check palindrome number
C program to check whether an alphabet is a vowel or consonant
Program to find square root of a number in C
C program to check whether a number is positive or negative