Tuesday, September 15, 2015

Filled Under:

C program to find the roots of a quadratic equation

Share
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,x1,x2,d;
clrscr();
printf("Enter the values a, b and c for the equation ax^2+bx+c=0:\n");
scanf("%f%f%f",&a,&b,&c);
d=(b*b)-(4*a*c);
if(d==0)
{
x1=x2=(-b)/(2*a);
printf("Roots are real and equal, x1=x2=%f",x1);
}
else if (d>0)
{
x1=((-b)+sqrt(d))/(2*a);
x2=((-b)-sqrt(d))/(2*a);
printf("Roots are distinct, x1=%f, x2=%f",x1,x2);
}
else
printf("Roots are imaginary");
getch();
}

0 comments:

Post a Comment