0% found this document useful (0 votes)
39 views6 pages

C Program To Find All Roots of A Quadratic Equation Using Switch Case - Codeforwin

Find all roots

Uploaded by

Lightness Masero
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views6 pages

C Program To Find All Roots of A Quadratic Equation Using Switch Case - Codeforwin

Find all roots

Uploaded by

Lightness Masero
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1/13/22, 8:02 AM C program to find all roots of a quadratic equation using switch case - Codeforwin

Learn C programming, Data Structures tutorials, exercises, examples, programs, hacks, tips and tricks online.

C program to find all roots of a quadratic


equation using switch case
April 13, 2016 Pankaj C programming C, Program, Switch

Programming Languages
Explore remote-first hiring
HackerRank vs DevSkiller IT testing
software | Tool reviews & comarission
devskiller.com

OPEN

Write a C program to find all roots of a Quadratic equation using switch case. How to find all roots of a
quadratic equation using switch case in C programming. Logic to calculate roots of quadratic equation in C
program.

Example
Input

Input a: 4
Input b: -2
Input c: -10

Output

Root1: 1.85
Root2: -1.35

Required knowledge
https://codeforwin.org/2016/04/c-program-to-find-all-roots-of-quadratic-equation-using-switch.html 1/6
1/13/22, 8:02 AM C program to find all roots of a quadratic equation using switch case - Codeforwin

Basic C programming, Relational operators, Switch case statement

Learn more - Program to find roots of quadratic equation using if...else.

Quadratic equation
In elementary algebra quadratic equation is an equation in the form of

Solving quadratic equation


A quadratic equation can have either one or two distinct real or complex roots depending upon nature of
discriminant of the equation. Where discriminant of the quadratic equation is given by

Depending upon the nature of the discriminant, formula for finding roots can be given as:

Case 1: If discriminant is positive. Then there are two real distinct roots given by.

Case 2: If discriminant is zero. Then it have exactly one real root given by.

Case 3: If discriminant is negative. Then it will have two distinct complex roots given by.

Logic to find roots of quadratic equation using


switch...case
Step by step descriptive logic to find roots of quadratic equation using switch case.

1. Input coefficients of quadratic equation. Store it in some variable say a , b and c .

2. Find discriminant of given equation using formula i.e. discriminant = (b * b) - (4 * a * c) .


You can also use pow() function to find square of b .

https://codeforwin.org/2016/04/c-program-to-find-all-roots-of-quadratic-equation-using-switch.html 2/6
1/13/22, 8:02 AM C program to find all roots of a quadratic equation using switch case - Codeforwin

3. Compute the roots based on the nature of discriminant. Switch the value of
switch(discriminant > 0) .

4. The expression (discriminant > 0) can have two possible cases i.e. case 0 and case 1 .

5. For case 1 means discriminant is positive. Apply formula


root1 = (-b + sqrt(discriminant)) / (2*a); to compute root1 and
root2 = (-b - sqrt(discriminant)) / (2*a); to compute root2 .

Learn more - Program to find square root of a number.

6. For case 0 means discriminant is either negative or zero. There exist one more condition to
check i.e. switch(discriminant < 0) .

7. Inside case 0 switch the expression switch(discriminant < 0) .

8. For the above nested switch there are two possible cases. Which is case 1 and case 0 . case 1
means discriminant is negative. Whereas case 0 means discriminant is zero.

9. Apply the formula to compute roots for both the inner cases.

Program to find roots of quadratic equation


using switch...case

Internships In 70+
Countries
Graduate With Up To One Year Of Work
Experience With ALU’s Global Internship
Program.

ALU Apply Now

1 /**
2 * C program to find all roots of a quadratic equation using switch case
3 */
4
5 #include <stdio.h>
6 #include <math.h> /* Used for sqrt() */
7
8 int main()
9 {
0 fl t b
https://codeforwin.org/2016/04/c-program-to-find-all-roots-of-quadratic-equation-using-switch.html 3/6
1/13/22, 8:02 AM C program to find all roots of a quadratic equation using switch case - Codeforwin
10 float a, b, c;
11 float root1, root2, imaginary;
12 float discriminant;
13
14 printf("Enter values of a, b, c of quadratic equation (aX^2 + bX + c
15 scanf("%f%f%f", &a, &b, &c);
16
17 /* Calculate discriminant */
18 discriminant = (b * b) - (4 * a * c);
19
20
21 /* Compute roots of quadratic equation based on the nature of discri
22 switch(discriminant > 0)
23 {
24 case 1:
25 /* If discriminant is positive */
26 root1 = (-b + sqrt(discriminant)) / (2 * a);
27 root2 = (-b - sqrt(discriminant)) / (2 * a);
28
29 printf("Two distinct and real roots exists: %.2f and %.2f",
30 root1, root2);
31 break;
32
33 case 0:
34 /* If discriminant is not positive */
35 switch(discriminant < 0)
36 {
37 case 1:
38 /* If discriminant is negative */
39 root1 = root2 = -b / (2 * a);
40 imaginary = sqrt(-discriminant) / (2 * a);
41
42 printf("Two distinct complex roots exists: %.2f + i%
43 root1, imaginary, root2, imaginary);
44 break;
45
46 case 0:
47 /* If discriminant is zero */
48 root1 = root2 = -b / (2 * a);
49
50 printf("Two equal and real roots exists: %.2f and %.
51
52 break;
53 }
54 }
55
56 return 0;
57 }

Output   
https://codeforwin.org/2016/04/c-program-to-find-all-roots-of-quadratic-equation-using-switch.html 4/6
1/13/22, 8:02 AM C program to find all roots of a quadratic equation using switch case - Codeforwin

Enter values of a, b, c of quadratic equation (aX^2 + bX + c): 4 -2


-10
Two distinct and real roots exists: 1.85 and -1.35

Happy coding 😉

Recommended posts
Switch case programming exercise index
C program to print day of week name using switch case.
C program to find total number of days in a month using switch case.
C program to check positive, negative or zero using switch case.
C program to find maximum between two numbers using switch case.
C program to check whether a number is even or odd using switch case.
C program to create simple calculator using switch case.

About Pankaj
Pankaj Prakash is the founder, editor and blogger at Codeforwin. He loves to learn new techs and write
programming articles especially for beginners. He works at Vasudhaika Software Sols. as a Software
Design Engineer and manages Codeforwin. In short Pankaj is Web developer, Blogger, Learner, Tech and
Music lover.

Follow on: Twitter | Google | Website or View all posts by Pankaj

https://codeforwin.org/2016/04/c-program-to-find-all-roots-of-quadratic-equation-using-switch.html 5/6
1/13/22, 8:02 AM C program to find all roots of a quadratic equation using switch case - Codeforwin

Have a doubt, write here. I will help my best.


Before commenting you must escape your source code before commenting. Paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>

https://codeforwin.org/2016/04/c-program-to-find-all-roots-of-quadratic-equation-using-switch.html 6/6

You might also like