Created
April 23, 2020 15:37
-
-
Save Redeem-Grimm-Satoshi/d89e53665205c68a5905cc571c7e7cd5 to your computer and use it in GitHub Desktop.
Solve Quadratic Equation Using Numerical Approach
This file contains hidden or 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
/* | |
Author: Redeem Grimm | |
Date: April 20 | |
Purpose: Solves Quadratic Equation Using Numerical Approach | |
*/ | |
//variable declaration | |
float a,b,c,rRoot,root1,root2,discriminant,compute1,compute2; | |
#include<stdio.h> | |
#include<stdlib.h> | |
#include<math.h> | |
int main(){ | |
//prompting the user and getting input | |
printf("Enter The Three Constants: "); | |
scanf("%f%f%f",&a,&b,&c); | |
//discriminant | |
discriminant=b*b-4*a*c; | |
//Conditions Before Various Operations Are Carried Out | |
if(discriminant>0){ | |
//execute equalDistinctRoots | |
equalDistinctRoots(); | |
}else if(discriminant==0){ | |
//execute equalRoots | |
equalRoots(); | |
}else{ | |
//execute complexRoots | |
complexRoots(); | |
} | |
return 0; | |
} | |
//Methods To Carry Out Different Operations | |
void equalDistinctRoots(){ | |
root1=(-b + sqrt(discriminant))/(2*a); | |
root2=(-b - sqrt(discriminant))/(2*a); | |
printf("Status: Roots Are Real And Distinct.\n"); | |
printf("Root1: %.2f\n",root1); | |
printf("Root2: %.2f\n",root2); | |
} | |
void equalRoots(){ | |
rRoot=-b/(2*a); | |
printf("Status: Roots Are Real And Equal.\n"); | |
printf("Root1 = Root2 = %.2f\n",rRoot); | |
} | |
void complexRoots(){ | |
compute1=sqrt(-discriminant)/(2*a); | |
rRoot=-b/(2*a); | |
printf("Status: Roots Are Complex.\n"); | |
printf("Root1: %.2f + %.2fi\n",rRoot,compute1); | |
printf("Root2: %.2f - %.2fi\n",rRoot,compute1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So Simplified!