Quadratic Equation Program in C

Flow Control
If-else Statement in C
Programs on if-else
Switch Case in C
Switch case Programs
Conditional Operator
While loop in C
Do-while loop in C
While vs do-while
For loop in C
Break keyword in C
Continue keyword in C
Break vs Exit in C
Goto keyword in C
☕️ Flow Control Programs
Largest in 3 Numbers
Find Grade of student
Find the absolute value
Vowel or Consonant
Leap Year Program
Simple calculator in C
Check Odd or Even 
Roots of Quadratic Equation
Find Reverse of Number
Factors of a number in C
Generate Multiplication table
Find Power of a Number
Find GCD and LCM
Find factorial of Number
Count Number of Digits
Sum of digits in Number
Sum of N Natural Numbers
Sum of Squares of Natural No.
Find Sum of Odd Numbers
Find the Sum of Series
Find Fibonacci series in C
Sum of the Fibonacci series
Sum until enters +ve numbers
Sum of max 10 no. & Skip -ve
☕️ C Conversion Programs
Celsius to Fahrenheit
Fahrenheit to Celsius
Decimal ↔ Binary
Decimal ↔ Octal
Octal ↔ Binary in C
☕️ Number Programs in C
Prime Number in C
Strong Number in C
Krishnamurthy Number
Neon Number in C
Palindrome number
Perfect Number in C
Armstrong Number
☕️ Pattern Programs in C
Pattern programs in C
Printing pattern using loops
Floyd’s triangle Program
Pascal Triangle Program
Pyramid Star Pattern in C
Diamond Pattern Programs
Half Diamond pattern in C
Print Diamond Pattern
Hollow Diamond Pattern
Diamond Pattern of Numbers

A quadratic equation is an equation of the second degree, meaning it contains at least one term that is squared. The standard form of the quadratic equation is ax² + bx + c = 0 where a, b, and c are real and a !=0, x is an unknown variable. The nature of roots is determined by the discriminant. Here we will write a quadratic equation program in c to solve quadratic equation and find the roots of quadratic equation using c programming language.

Prerequisites for Quadratic Equation Program in C:- If-else statement in C, programs on if-else in C, conditional operator in C

The discriminant of the Quadratic equation is calculated as b²-4*a*c

discriminant(d) = b²-4*a*c

The nature of the roots are given as,

  • If discriminant>1 then the roots are real and different
  • If discriminant=0 then the roots are real and equal
  • discriminant<1 then the roots are complex and different
D = b²-4*a*cNature of roots
D > 1roots are real and different.
D = 0roots are real and equal.
D < 1roots are complex and different.

For the quadratic equation ax² + bx + c = 0, if we denote the discriminant as d, then their roots

If d > 1 then
root1 = (-b + √d) / 2a
root2 = (-b - √d) / 2a

If d = 0 then both roots are -b / 2a

If d < 1 then
root1 = -b / 2a + i (√d / 2a)
root2 = -b / 2a - i (√d / 2a)

C program to solve the quadratic equation

#include<stdio.h>
#include<math.h>
int main()
{
   int a, b, c, d;
   int root1, root2, realPart, imaginaryPart;

   printf("Enter cofficients (a, b, and c): ");
   scanf("%d %d %d", &a, &b, &c);

   printf("The quadratic equation: %dx²%+dx%+d \n",a,b,c);
   d = (b*b) - 4*a*c; //discriminant

   if( d>1 )
   {
     root1 = (-b+sqrt(d)) / 2*a;
     root2 = (-b-sqrt(d)) / 2*a;
     printf("Roots are %d and %d\n", root1, root2);
   }
   else if(d==0)
   {
     root1 = root2 = -b/2*a;
     printf("Roots are %d and %d\n", root1, root2);
   }
   else
   {
     realPart = -b/2*a;
     imaginaryPart = sqrt(d)/2*a;
     printf("root1 = %d + i(%d)\n", realPart, imaginaryPart);
     printf("root1 = %d - i(%d)\n", realPart, imaginaryPart);
   }
   return 0;
}

Output for different test cases:-

Enter cofficients (a, b, and c): 1 -1 -6
The quadratic equation: 1x²-1x-6
Roots are 3 and -2

Enter cofficients (a, b, and c): 1 -12 36
The quadratic equation: 1x²-12x+36
Roots are 6 and 6

Enter cofficients (a, b, and c): 1 4 5
The quadratic equation: 1x²+4x+5
root1 = -2 + i(-2147483648)
root1 = -2 – i(-2147483648)

In the quadratic equation program in C, the variables a, b, and c are the coefficient of the quadratic equation, and the variable d is its discriminant. To find the discriminant (d) we used sqrt() function defined in the math.h standard library. The sqrt() function finds the square root value of a number. The nested if-else statement is used for the nature of the roots.

Note:- Linux/Unix user may get an error:- Undefined reference to sqrt() in C Programming (or other mathematical functions) even includes math.h header. Windows user doesn’t get any warning and error but Linux/Unix user may get an undefined reference to sqrt. This is a linker error. To solve this problem use -lm during compilation.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Flow Control
If-else Statement in C
Programs on if-else
Switch Case in C
Switch case Programs
Conditional Operator
While loop in C
Do-while loop in C
While vs do-while
For loop in C
Break keyword in C
Continue keyword in C
Break vs Exit in C
Goto keyword in C
☕️ Flow Control Programs
Largest among 3 Numbers
Find Grade of student
Find the absolute value
Check Vowel or Consonant
Leap Year Program
Simple calculator in C
Check Odd or Even 
Roots of Quadratic Equation
Find Reverse of Number
Factors of a number in C
Generate Multiplication table
Find Power of a Number
Find GCD and LCM
Find factorial of Number
Count Number of Digits
Sum of digits in Number
Sum of N Natural Numbers
Sum of Squares of Natural No.
Find Sum of Odd Numbers
Find the Sum of Series
Find Fibonacci series in C
Sum of the Fibonacci series
Sum until enters +ve numbers
Sum of max 10 no. & Skip -ve
☕️ C Conversion Programs
Celsius to Fahrenheit
Fahrenheit to Celsius
Decimal ↔ Binary
Decimal ↔ Octal
Octal ↔ Binary in C
☕️ Number Programs in C
Prime Number in C
Strong Number in C
Krishnamurthy Number
Neon Number in C
Palindrome number
Perfect Number in C
Armstrong Number
☕️ Pattern Programs in C
Pattern programs in C
Printing pattern using loops
Floyd’s triangle Program
Pascal Triangle Program
Pyramid Star Pattern in C
Diamond Pattern Programs
Half Diamond pattern in C
Print Diamond Pattern
Hollow Diamond Pattern
Diamond Pattern of Numbers

Leave a Reply