Find the Roots of a Quadratic Equation



Sumit Kar, Sorting, C, Timus Rak, C Program



#include <stdio.h> 
#include <math.h>

int main() {
float a, b, c, determinant, r1,r2, real, imag;

printf("Enter coefficients a, b and c: ");
scanf("%f%f%f",&a,&b,&c);

determinant=b*b-4*a*c;
if (determinant>0) {
r1= (-b+sqrt(determinant))/(2*a);
r2= (-b-sqrt(determinant))/(2*a);
printf("Roots are: %.2f and %.2f",r1 , r2);
}
else if (determinant==0) {
r1 = r2 = -b/(2*a);
printf("Roots are: %.2f and %.2f", r1, r2);
} else {
real= -b/(2*a);
imag = sqrt(-determinant)/(2*a);
printf("Roots are: %.2f+%.2fi and %.2f-%.2fi", real, imag, real, imag);
}
return 0;
}

/*
Summary: Finds roots of a quadratic equation ax2+bx+c=0 where a, b and c are coefficients. This program will ask the coefficients: a, b and c from user and displays the roots.
*/


Comments

Popular posts from this blog

Write a Program to Add two 3x3 Matrix using C

C program for Unit Conversion

Write a Program to Add two 5x5 Matrix using C