C Tutorial
#include<stdio.h> #include<math.h> void main() { float a, b, c, discriminant, root1, root2; printf("Input values of a, b and c:\n"); scanf("%f %f %f", &a, &b, &c); printf("a: %f\nb: %f\nc: %f\n",a,b,c); discriminant = b*b - 4*a*c; if(discriminant < 0) { printf("\n\nRoots are imaginary\n"); } else { root1 = (-b + sqrt(discriminant)/2.0*a); root2 = (-b - sqrt(discriminant)/2.0*a); printf("\n\nRoot1 = %5.2f\n\nRoot2 = %5.2f\n", root1, root2); } }Output:
$ cc quadratic-equation.c -lm $ ./a.out Input values of a, b and c: 2 4 -6 a: 2.000000 b: 4.000000 c: -6.000000 Root1 = 4.00 Root2 = -12.00 $ ./a.out Input values of a, b and c: 1 2 3 a: 1.000000 b: 2.000000 c: 3.000000 Roots are imaginary
C Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us
| Report website issues in Github
| Facebook page
| Google+ page