Skip to content

Instantly share code, notes, and snippets.

@feynon
Created October 10, 2018 14:58
Show Gist options
  • Save feynon/352e20fa263e684bfd14e5479c2e6c77 to your computer and use it in GitHub Desktop.
Save feynon/352e20fa263e684bfd14e5479c2e6c77 to your computer and use it in GitHub Desktop.
Program to find roots of quadratic expression in C.
//To find quadratic roots of an equation
#include <stdio.h>
#include <math.h>
int main()
{
float a, b, c;
float d, r1, r2;
printf("Enter quadratic equation in the format ax^2+bx+c: ");
scanf("%fx^2%fx%f",&a,&b,&c);
d = b * b - 4 * a * c;
if(d < 0){
printf("Roots are complex numbers.\n")
return 0;
}
else if(d==0){
printf("Roots are equal.\n");
r1 = - b / (2 * a);
printf("Roots of quadratic expression is: %.3f "r1);
}
else{
printf("Roots are real numbers.\n");
r1 = (-b + sqrt(d)) / 2 * a);
r2 = (-b - sqrt(d)) / 2 * a);
printf("Roots of the equation are: %.3f ,%.3f" r1, r2);
}
return 0;
}
@feynon
Copy link
Author

feynon commented Oct 10, 2018

Please debug and correct the given code and paste the same on paste https://hastebin.com,Thanks! I'm live @ankeshb8 on twitter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment