- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to write a C program to find the roots of a quadratic equation?
Problem
Applying the software development method to solve any problem in C Language
Solution
- Find roots of a quadratic equation, ax2+bx+c.
- There will be 2 roots for given quadratic equation.
Analysis
Input − a,b,c values
Output − r1, r2 values
Procedure
$r_{1}=\frac{-b+\sqrt{b^2-4ac}}{2a}$
$r_{2}=\frac{-b-\sqrt{b^2-4ac}}{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
- compute r1 = -b/2a, r2=-b/2a
- print r1,r2 values
- Otherwise if d < 0 then print roots are imaginary
- Stop
Implementation Code
# include<stdio.h> # include<conio.h> # include<math.h> main (){ float a,b,c,r1,r2,d; printf (“enter the values of a b c”); scanf (“ %f %f %f”, &a, &b, &c); 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”, r1, r2); } else if (d= =0){ r1 = -b/(2*a); r2 = -b/(2*a); printf (“roots are equal =%f %f”, r1, r2); } else printf(“Roots are imaginary”); getch (); }
Testing
Case 1: enter the values of a b c: 1 4 3 r1 = -1 r2 = -3 Case 2: enter the values of a b c: 1 2 1 r1 = -1 r2 = -1 Case 3: enter the values of a b c: 1 1 4 Roots are imaginary
- Related Articles
- C++ Program to Find All Roots of a Quadratic Equation
- C program to find the Roots of Quadratic equation
- Java program to find the roots of a quadratic equation
- Java Program to Find all Roots of a Quadratic Equation
- Kotlin Program to Find all Roots of a Quadratic Equation
- Haskell program to find all roots of a quadratic equation
- How to Find all Roots of a Quadratic Equation in Golang?
- Finding roots of a quadratic equation – JavaScript
- Program to find number of solutions in Quadratic Equation in C++
- Write all the values of k for which the quadratic equation $x^2+kx+16=0$ has equal roots. Find the roots of the equation so obtained.
- Find the quadratic roots in the equation$4x^{2}-3x+7$
- If $1$ is a root of the quadratic equation $3x^2 + ax - 2 = 0$ and the quadratic equation $a(x^2 + 6x) - b = 0$ has equal roots, find the value of b.
- If $2$ is a root of the quadratic equation $3x^2 + px - 8 = 0$ and the quadratic equation $4x^2 - 2px + k = 0$ has equal roots, find the value of k.
- Find the roots of the quadratic equation $sqrt{2}x^{2}+7x+5sqrt{2}=0$.
- Find the roots of the following quadratic equation:$x^{2} -3sqrt {5} x+10=0$

Advertisements