Program to Find Roots of quadratic equation in C Language
Description:
In this program, We are going to find the roots of quadratic equation using a C program. We are going to use the Quadratic Formula to find the roots.
Learn More about Quadratic Equation at – Quadratic Equations (mathsisfun.com)
Quadratic Formula to calculate the roots of Quadratic Equation:
PreRequisites:
We need to have a basic understanding of the if else statement in C. Please go through the following post to learn more.
Roots of quadratic equation in C Language
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include<stdio.h> #include<math.h> int main() { float a,b,c,root1,root2; printf("Enter values of a,b,c for finding roots of a quadratic eq: "); scanf("%f%f%f",&a,&b,&c); /*checking condition*/ if(b*b > 4*a*c) { root1=(-b+sqrt(b*b-4*a*c))/2*a; root2=(-b-sqrt(b*b-4*a*c))/2*a; printf("*****ROOTS ARE***** \n"); printf("root1=%f \nroot2=%f \n",root1,root2); } else printf("Imaginary Roots \n"); return 0; } |
Program Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
venkey@venkey$ gcc roots.c venkey@venkey$ ./a.out Enter values of a,b,c for finding roots of a quadratic eq: 4 25 3 *****ROOTS ARE***** root1=-1.958351 root2=-98.041649 venkey@venkey$ ./a.out Enter values of a,b,c for finding roots of a quadratic eq: 5 100 10 *****ROOTS ARE***** root1=-2.512627 root2=-497.487366 venkey@venkey$ ./a.out Enter values of a,b,c for finding roots of a quadratic eq: 8 566 1 *****ROOTS ARE***** root1=-0.113077 root2=-4527.886719 venkey@venkey$ ./a.out Enter values of a,b,c for finding roots of a quadratic eq: 1 2 3 Imaginary Roots venkey@venkey$ |
Related Articles:
- Modulo operator in C explained with Examples.
- In depth examples on Arithmetic operators.
- Arithmetic operators in C language.
- Compilation Stages in C language.
- Identifiers in C language and Rules for naming Identifiers.
- Structure of C Language.
3 Responses
[…] Roots of Quadratic Equation Program […]
[…] C Program to Find Roots of quadratic equation in C Language […]
[…] C Program to Find Roots of quadratic equation in C Language […]