Home C Language Basic C Programs Quadratic Equation Program in C

Quadratic Equation Program in C

Write a C program to find the roots of a quadratic equation.

#include<stdio.h>

#include<math.h>

int main()

{

   int a,b,c;

   float d,r1,r2

   printf(“Enter A,B,C values :\n”);

   scanf(“%d%d%d”,&a,&b,&c);

   d=(b*b)-(4*a*c);

   if(d==0)

   {

      r1=r2=-b/(2.0*a);

      printf(“The roots are real and equal\n”);

      printf(“Roots are : %.2f\t%.2f\n”,r1,r2);

   }

   else if(d>0)

   {

      r1=(-b+sqrt(d))/(2.0*a);

      r2=(-b-sqrt(d))/(2.0*a);

      printf(“The roots are real and not equal\n”);

   printf(“Roots are : %.2f\t%.2f\n”,r1,r2);

   }

   else

      printf(“The roots are imaginary\n”);

   return 0;

}

Output:

i)Enter A,B,C values :

1 2 1

The roots are real and equal

Roots are : -1.00 -1.00

ii)Enter A,B,C values :

5 2 5

The roots are imaginary