Last active
December 22, 2015 02:08
-
-
Save kylelk/6400930 to your computer and use it in GitHub Desktop.
Quadratic Equation roots
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Program to find roots of a quadratic equation when coefficients are entered by user. */ | |
/* Library function sqrt() computes the square root. */ | |
#include <stdio.h> | |
#include <math.h> /* This is needed to use sqrt() function.*/ | |
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment