A C program to find Roots of Quadratic equation

First, the program accepts 3 values and then uses the expression b2 – 4ac is called the discriminant. The value of the discriminant shows how many roots f(x) has: - If b2 – 4ac > 0 then the quadratic function has two distinct real roots. - If b2 – 4ac = 0 then the quadratic function has one repeated real root.

Then if the result, lets name it d. If d is less than 0 it uses the formula -b/(2*a). and if d is greater than 0 it uses the formula (-b +square root of d)/(2*a).

#include<stdio.h>
#include <math.h>
int main(){
  printf("Program to print the Quadratic Equation");
  float a,b,c,r1,r2,d;
  printf("Enter the value of a,b,c\n");
  scanf("%f%f%f",&a,&b,&c);
  d=b*b-4*a*c;
  if(d==0)
  {
    printf("Roots are real and equal\n");
    r1=-b/(2*a);
    r2=-b/(2*a);
    printf("Root1=%f,Root 2=%f\n",r1,r2);
  }
  else if(d>0)
  {
    printf("Roots are real and defined");
    r1=(-b+sqrt(d)/(2*a));
    r2=(-b-sqrt(d)/(2*a));
    printf("Root 1=%f",r1);
    printf("Root 2=%f",r2);
  }
  else{
    printf("Root ax are imaginary\n");
  }
}

This is a simple program to find the roots of a quadratic equation. This program uses math.h to find the sq root.

And don't forget to follow and share this blog with a friend who wants to learn programming.

Thank you...