How To Write A C Program To Find The Roots of A Quadratic Equation
How To Write A C Program To Find The Roots of A Quadratic Equation
JSP
iOS
SQL HTML CSS Javascript Python
HTML
C++ Programming
C#
Problem
Applying the software development method to
solve any problem in C Language.
Solution
ax2+bx+c.
Analysis
Input − a,b,c values
Procedure
−b+√b2−4ac
r1 = 2a
−b−√b2−4ac
r2 = 2a
Design (Algorithm)
Start
Read a, b, c values
Compute d = b2 4ac
if d > 0 then
r1 = b+ sqrt (d)/(2*a)
r2 = b sqrt(d)/(2*a)
Otherwise if d = 0 then
Stop
Implementation Code
# include<stdio.h>
# include<math.h>
int main () {
float a,b,c,r1,r2,d;
d= b*b - 4*a*c;
if (d>0) {
r1 = -b+sqrt (d) / (2*a);
r2 = -b-sqrt (d) / (2*a);
printf ("The real roots = %f %f",
}
else if (d==0) {
r1 = -b/(2*a);
r2 = -b/(2*a);
printf ("Roots are equal =%f %f",
}
else
printf("Roots are imaginary");
return 0;
}
Testing
Case 1:
Enter the values of a b c: 1 4 3
The real roots = -3.000000 -5.000000
Case 2:
Enter the values of a b c: 1 2 1
Roots are equal =-1.000000 -1.000000
Case 3:
Enter the values of a b c: 1 1 4
Roots are imaginary
Bhanu Priya
130K+ Views
Related Articles
C++ Program to Find All Roots of a
Quadratic Equation
Get Started
Advertisements
TOP TUTORIALS
Python Tutorial
Java Tutorial
C++ Tutorial
C Programming Tutorial
C# Tutorial
PHP Tutorial
R Tutorial
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
SQL Tutorial
TRENDING TECHNOLOGIES
Git Tutorial
Docker Tutorial
Kubernetes Tutorial
DSA Tutorial
SDLC Tutorial
Unix Tutorial
CERTIFICATIONS
DevOps Certification
Online Go Compiler
Online C Compiler
Online C# Compiler