Techy Blitz

The science of today is the technology of tomorrow.

Breaking

Wednesday, 31 January 2018

C program to Find the roots of a Quadratic Equation

Simple C program to find the roots of a Quadratic Equation :



Here's the code :

#include<stdio.h>
#include<math.h>
main()
{
 float a,b,c,d,x1,x2;
 printf("Enter the coefficients respectively:\n");
 scanf("%f%f%f",&a,&b,&c);
 d=(b*b)-(4*a*c);
 if(d>0)
 {
  printf("The roots are real\n");
  x1=(-b+sqrt(d))/2*a;
  x2=(-b-sqrt(d))/2*a;
  printf("The roots are %f\t and %f",x1,x2);
 }
 else if(d==0)
 {
  printf("The roots are equal\n");
  x1=(-b/2*a);
  printf("The roots are %f\t and %f",x1,x1);
 }
 else
 {
  d=(-1)*d;
  x1=(-b/2*a);
  x2=sqrt(d)/2*a;
  printf("The roots are imaginary\n");
  printf("The roots are %f+i%f and %f-i%f",x1,x2,x1,x2);
 }
}