c program to find the roots of a quadratic equation

Description


This program finds the roots of a quadratic equation. The roots are dependent on the discriminant. The program uses variable r to store this value. If the value of the discriminant is greater than 0, then there are two roots. If the value of the discriminant is equal to 0, then there is only one root. Otherwise the roots are imaginary. The program uses if else if statement to check for this conditions and prints the roots.

Program



#include<stdio.h>

#include<conio.h>

#include<math.h>

void main()

{

clrscr();

float a,b,c,r,r1,r2;

printf("enter the value for a,b&c");

scanf("%f%f%f",&a,&b,&c);

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

if (r>0)

{

r1=-b+sqrt(r)/(2*a);

r2=-b-sqrt(r)/(2*a);

printf("Roots are%f & %f",r1,r2);

}

else if(r==0)

{

r1=-b/(2*a);

printf("Root is%f",r1);

}

else

{

printf("Roots are imaginary");

}

getch();

}

No comments: