im just stuck at programming to solve quadratic equation,im doing this on C language,can anyone show a example C program to solve quadratic equation and if there is algorithm that would be a real help…
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people's questions, and connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Krishna Vijayan
You can use the program given below to find the roots of a quadratic equation. It’s a pretty simple program.
The output of the program will have two sections :
Program:
int main(){
float a,b,c;
float d,root1,root2;
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 number.\n”);
return 0;
}
root1 = ( -b + sqrt(d)) / (2* a);
root2 = ( -b – sqrt(d)) / (2* a);
printf(“Roots of quadratic equation will be: %.3f , %.3f”,root1,root2);
return 0;
}
Run it
shree
thanks for sharing this….
C Language is learned as a basic programming language in schools and engineering degrees for CSE Departments.
In this language, we think of logic and solve a question by writing programs and executing them. We execute every program to check whether it is correct syntactically as well as logically.
If you couldn’t solve quadratic equation with an algorithm in C program, don’t worry, I am here to help you out.
Algorithm:
The following steps have to be followed to write a program to solve the quadratic equation:
Program:
#include<stdio.in>
#include<math.h>
main()
{
int a,b,c,d,rt1,rt2;
printf(“Enter the values of a,b and c: “);
scanf(“%d%d%d”,&a,&b,&c);
d = pow(b,2) – 4*a*c;
if(d<0)
{
printf(“Imaginary Roots”);
printf(“\n rt1= %.3d% + .3fi\n”, -b/(2*a),sqrt(-d)/(2*a));
printf(“\n rt2= %.3d% + .3fi\n”, -b/(2*a),sqrt(-d)/(2*a));
}
else
{
rt1 = (-b + sqrt(d))/(2*a);
rt2 = (-b – sqrt(d))/(2*a);
printf(“First root = %d and Second root = %d”,rt1,rt2);
}
return 0;
}
All the steps mentioned above which are involved in the algorithm and program are for finding or solving the quadratic equation in C language.
If you found this answer useful, make sure to like it. Also, share it with people who are going to write a program in C to solve the quadratic equation.
arunraj503
im dropping the code to solve quadratic with absolute C program.
you can copy this quadratic solution program in c if you want.
#include <stdio.h>
#include <math.h>
int main() {
double a, b, c;
double discriminant, root1, root2;
printf(“Enter the coefficients a, b, and c: “);
scanf(“%lf %lf %lf”, &a, &b, &c);
// Calculate the discriminant
discriminant = b * b – 4 * a * c;
if (discriminant > 0) {
// Two real and distinct roots
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b – sqrt(discriminant)) / (2 * a);
printf(“Root 1 = %.2lf and Root 2 = %.2lf\n”, root1, root2);
} else if (discriminant == 0) {
// One real root (double root)
root1 = -b / (2 * a);
printf(“Double Root = %.2lf\n”, root1);
} else {
// Complex roots
double realPart = -b / (2 * a);
double imaginaryPart = sqrt(-discriminant) / (2 * a);
printf(“Complex Roots: %.2lf + %.2lfi and %.2lf – %.2lfi\n”, realPart, imaginaryPart, realPart, imaginaryPart);
}
return 0;
}
see the solution for factorial of a number in c.