A Learning Platform

SUCCESS

Success = Discipline + Consistency + Sacrifice.
Discipline - in what you do.
Consistency - in what you do.
Sacrifice - for what you do.

                   -- Manjunatha C. P. --

Roots of Quadratic equation(if else ladder) C Program

Quadratic equation is the polygonal equation of degree 2. Quadratic equations are represented by equation ax2 + bx + c = 0 , where a, b and c are coefficients.

Now let us solve the quadratic equation ax2 + bx + c = 0
Formula to solve quadratic equation:



Nature of roots:

Discriminant determines the nature of roots.

Discriminant(d) = b * b – 4 * a * c

if d = 0 , then roots are real and equal.
if d > 0 , then roots are real and distinct and
if d< 0 , then roots are imaginary.

In the below section we are going to write an algorithm and c program to calculate the roots of quadratic equation using if else statement.

Description:

a, b and c – Coefficients of quadratic equation.
d – Discriminant.
r1 and r2 – Roots of quadratic equation.
real – Real part of roots.
img – Imaginary part of roots.
Input : Coefficients a, b and c.
Output : Roots of quadratic equation.

Algorithm to find the roots of quadratic equation using if else ladder

STEP 1: START
STEP 2: READ the coefficients a, b and c
              READ a, b, c
STEP 3: CALCULATE the value of discriminant
              d = b * b – 4 * a * c         
 STEP 4: CHECK for nature of roots using discriminant
STEP 5: ROOTS are real and distinct if d > 0
              if d > 0
              r1 = (-b + sqrt(d)) / (2 * a)
              r2 = (-b – sqrt(d)) / (2 * a)
              PRINT r1, r2
STEP 6: ROOTS are real and equal if d = 0
              if d = 0
              r1 = r2 = -b / (2 * a)
              PRINT r1, r2
STEP 7: ROOTS are imaginary if d < 0
              if d < 0
              real = -b / (2 * a);
              img = sqrt(-d) / (2 * a)
              PRINT r1, r2
STEP 8: END

C Program to find the roots of quadratic equation using if else ladder

#include<stdio.h>
#include<conio.h>
#include<math.h>
int main() {
    float a, b, c, d, r1, r2, real, img;
    printf("Enter coefficients a, b and c: ");
    scanf("%f %f %f", &a, &b, &c);
    d = b * b - 4 * a * c;
    if (d > 0) {
        printf("Roots are real and distinct\n");
        r1 = (-b + sqrt(d)) / (2 * a);
        r2 = (-b - sqrt(d)) / (2 * a);
        printf("root1 = %f and root2 = %f", r1, r2);
    }
    else if (d == 0) {
        printf("Roots are real and equal\n");
        r1 = r2 = -b / (2 * a);
        printf("root1 = root2 = %f", r1);
    }
    else {
        printf("Roots are imaginary\n");
        real = -b / (2 * a);
        img = sqrt(-d) / (2 * a);
        printf("root1 = %f+i%f and root2 = %f-i%f", real, img, real, img);
    }
    getch();
    return 0;

Output 1:

Enter coefficients a, b and c: 1 4 3
Roots are real and distinct
root1 = -1.000000 and root2 = -3.000000

Output 2:

Enter coefficients a, b and c: 2 4 2
Roots are real and equal
root1 = root2 = -1.000000

Output 3:

Enter coefficients a, b and c: 2.3 4 5.6
Roots are imaginary
root1 = -0.869565+i1.295623 and root2 = -0.869565-i1.295623

QUIZ:

1} if else ladder is a
  1. Looping statement.
  2. Branching statement.
  3. Input statement.
  4. Output statement.
2] Which of the following are decision making and branching statements?
  1. if statement.
  2. if else statement.
  3. if else ladder statement.
  4. Above all of them.