Advertisement
Kalhnyxtakias

Untitled

Nov 2nd, 2020
2,069
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.98 KB | None | 0 0
  1. /*A simple program that calculates the roots of a quadratic equation, using the coefficients as input, and outputing the roots*/
  2. /*ALEXANDROS AGRAFIOTIS,ECE UTH,02/11/2020*/
  3.  
  4. #include <stdio.h>
  5. #include <math.h> /* Needed for sqrt function */
  6.  
  7. int main(int argc, char* argv[]){
  8.     double quadratic,linear,constant,root1,root2; /*Coefficients and roots*/
  9.  
  10.     printf("\nCoefficients: ");
  11.  
  12.     printf("\nQuadratic: "); /* Alias : a */
  13.     scanf("%lf",&quadratic);
  14.  
  15.     printf("\nLinear: "); /* Alias : b */
  16.     scanf("%lf",&linear);
  17.  
  18.     printf("\nConstant: "); /* Alias : c */
  19.     scanf("%lf",&constant);
  20.  
  21.     /*Calculate roots*/
  22.     /* Formula = (-b ± sqrt(b*b - 4*a*c)) / 2*a */
  23.     root1 = (-linear + sqrt(linear*linear - 4*quadratic*constant)) / (2*quadratic);
  24.     root2 = (-linear - sqrt(linear*linear - 4*quadratic*constant)) / (2*quadratic) ;
  25.  
  26.     printf("\nRoots: %09.4lf, %09.4lf\n",  (root1<root2)? root1 : root2, (root1>root2)? root1 : root2);
  27.  
  28.     return(0);
  29. }
  30.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement