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…

# c program to solve quadratic equation with algorithm

Share

## 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.