Solution of Quadratic Equations Using C
This problem makes you clear about writing Math functions in C. Here I use sqrt() funcitons to calculate the square root. This is the simple program and calculates only real roots. The complete source code and output is given below:
1: #include<stdio.h> 2: #include<math.h> //must be included 3: 4: int main(){ 5: float a,b,c,dscr,root1,root2; 6: printf("Enter the value of a, b and c: "); 7: scanf("%f%f%f",&a,&b,&c); 8: dscr = pow(b,2) - 4*a*c; 9: if(dscr < 0){ 10: printf("\nRoots are imaginary\n"); 11: }else{ 12: root1 = (-b + sqrt(dscr))/(2.0*a); 13: root2 = (-b - sqrt(dscr))/(2.0*a); 14: printf("\nRoot1 = %f\nRoot2 = %f",root1,root2); 15: } 16: 17: return 0; 18: } 19:
output