Quadratic equation solving in C
The Quadratic Formula:
For ax2 + bx + c = 0, the value of x is given by:
Program
#include<stdio.h> #include<conio.h> #include<math.h> main() { double a,b,c,d,x1,x2; printf("Enter the co-efficient value of a:"); scanf("%lf",&a); printf("Enter the co-efficient value of b:"); scanf("%lf",&b); printf("Enter the co-efficient value of c:"); scanf("%lf",&c); d=(b*b-4*a*c); if(d>0) { printf("\nRoots are real"); x1=(-b+sqrt(d))/(2*a); x2=(-b-sqrt(d))/(2*a); printf("\nx=%.3lf , %.3lf",x1,x2); } else if(d==0) { printf("\nRoots are equal"); x1=(-b+sqrt(d))/(2*a); printf("\nx=%.3lf",x1); } else { printf("\nRoots are imaginary"); } getch(); }
Output
screenshot of program for quadratic equation |
Related programs
No comments