Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*A simple program that calculates the roots of a quadratic equation, using the coefficients as input, and outputing the roots*/
- /*ALEXANDROS AGRAFIOTIS,ECE UTH,02/11/2020*/
- #include <stdio.h>
- #include <math.h> /* Needed for sqrt function */
- int main(int argc, char* argv[]){
- double quadratic,linear,constant,root1,root2; /*Coefficients and roots*/
- printf("\nCoefficients: ");
- printf("\nQuadratic: "); /* Alias : a */
- scanf("%lf",&quadratic);
- printf("\nLinear: "); /* Alias : b */
- scanf("%lf",&linear);
- printf("\nConstant: "); /* Alias : c */
- scanf("%lf",&constant);
- /*Calculate roots*/
- /* Formula = (-b ± sqrt(b*b - 4*a*c)) / 2*a */
- root1 = (-linear + sqrt(linear*linear - 4*quadratic*constant)) / (2*quadratic);
- root2 = (-linear - sqrt(linear*linear - 4*quadratic*constant)) / (2*quadratic) ;
- printf("\nRoots: %09.4lf, %09.4lf\n", (root1<root2)? root1 : root2, (root1>root2)? root1 : root2);
- return(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement