Write a c program to find roots of a quadratic equation
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x1,x2,imaginarypart,realpart,discriminant,a,b,c;
clrscr();
printf("Enter the values of a, b, c: ");
scanf("%f%f%f",&a,&b,&c);
discriminant=b*b-4*a*c;
if(discriminant>0)
{
x1=(-b+sqrt(discriminant))/(2*a);
x2=(-b-sqrt(discriminant))/(2*a);
printf("Roots are real and different:\n");
printf("x1=%f and x2=%f",x1,x2);
}
else if(discriminant==0)
{
x1=-b/(2*a);
printf("Roots are real and equal\n");
printf("x1=x2=%f",x1);
}
else
{
realpart=-b/(2*a);
imaginarypart=sqrt(-discriminant)/(2*a);
printf("Roots are imaginary\n");
printf("%f+%fi",realpart,imaginarypart);
printf("%f-%fi",realpart,imaginarypart);
}
getch();
}
Output
run 1:
Enter the values of a, b, c: 1
2
3
Roots are imaginary
-1.000000+1.414214i-1.000000-1.414214i
run 2:
Enter the values of a, b, c: 1
5
6
Roots are real and different:
x1=-2.000000 and x2=-3.000000
run 3:
Enter the values of a, b, c: 1
4
4
Roots are real and equal
x1=x2=-2.000000
No comments: