Find the Roots of a Quadratic Equation in C Programming

C program to find Roots of a Quadratic Equation.

In this program, you will learn to find the roots of a quadratic equation in c programming.

The standard form of a quadratic equation is:

ax2 + bx + c = 0, where
a, b and c are real numbers and a!=0

The terms b2; - 4ac is known as the discriminant of a quadratic equation. It tells the nature of the roots.

1.    If the discriminant is greater than o, the roots are real and different.
2.    If the discriminant is less than o, the roots are complex and different.
3.    If the discriminant is equal to o, the roots are real and equal.

Steps to find the square roots of the quadratic equation.

1.    Initialize all the variables used in the quadratic equation.
2.    Take inputs of all coefficient variables x, y and z from the user.
3.    And then, find the discriminant of the quadratic equation using the formula: Discriminant = (y*y)-(4*x*z).
4.    Calculate the roots based on the nature of the discriminant of the quadratic equation.
5.    If discriminant > 0, then
       Root1 = (-y + sqrt(det)) / (2*x)
       Root2 = (-y + sqrt(det)) / (2*x)
       print the roots are real and distinct.
6.    Else if (discriminant = 0) then,
       Root1 = Root2 = -y / (2*x).
       print both roots are real and equal.
7.    Else (discriminant < 0), the roots are distinct complex where,
       Real part of the root is: Root1 = Root2 = -y / (2*x) or real = -y / (2*x).
       Imaginary part of the root is: sqrt( -discriminant)/ (2*x).
       print both roots are imaginary, where first root is (r + i) img and second root is (r - i) img.
8.    Exit ot terninate the program.

  •  Program:

#include <math.h>
#include <stdio.h>
int main() {
    double a, b, c, discriminant, root1, root2, realPart, imagPart;
    printf("Enter coefficients a, b and c: ");
    scanf("%lf %lf %lf", &a, &b, &c);

    discriminant = b * b - 4 * a * c;

    // condition for real and different roots
    if (discriminant > 0) {
        root1 = (-b + sqrt(discriminant)) / (2 * a);
        root2 = (-b - sqrt(discriminant)) / (2 * a);
        printf("root1 = %.2lf and root2 = %.2lf", root1, root2);
    }

    // condition for real and equal roots
    else if (discriminant == 0) {
        root1 = root2 = -b / (2 * a);
        printf("root1 = root2 = %.2lf;", root1);
    }

    // if roots are not real
    else {
        realPart = -b / (2 * a);
        imagPart = sqrt(-discriminant) / (2 * a);
        printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart, imagPart, realPart, imagPart);
    }

    return 0;
} 

  •  Output:

Enter coefficients a, b and c: 2.3
4
5.6
root1 = -0.87+1.30i and root2 = -0.87-1.30i

Pseudo code of the Quadratic Equation

  1. Start
  2. Input the coefficient variable, x, y and z.
  3. D <- sqrt (y*y-4*x*z).
  4. R1 <- (-y+D)/(2*x).
  5. R2 <- (-y-D)/(2*x).
  6. Print the roots R1 and R2.
  7. Stop

  • Visit:

Write a C program to computer Fahrenheit from centigrade ( f=1.8*c+32 ) Here
Write a C program to find out distance travelled by the equation d=ut+at^2 Here
Write a C program to find that the accepted number is negative or positive or zero Here
Write a program to read mark of a student from keyboard the student is pass or fail ( using if else ) Here
Write a program to read three numbers from keyboard and find out maximum out of these three. (nested if else) Here
Write a program to check whether the entered character is capital, small letter, digit or any special character Here
Write a program to read marks from keyboard and your program should display equivalent grade according to following table (if else ladder) Here
Write a C program to prepare pay slip using following data Here
Write a C program to read no 1 to 7 and print relatively day Sunday to Saturday Here
Write a C program to find out the Maximum and Minimum number from given 10 numbers Here
Write a C program to input an integer number and check the last digit of number is even or odd Here 
Write a C program to find factorial of a given number Here
Write a C program to reverse a number Here
Write a C program to generate first n number of Fibonacci series Here
Write a C program to find out sum of first and last digit of a given number Here
Write a C program to find the sum and average of different numbers Here
Write a program to calculate average and total of 5 students for 3 subjects Here
Read five persons height and weight and count the number of person having height greater than 170 and weight less than 50 Here
Write a program to check whether the given number is prime or not Here
Write a program to evaluate the series 1^2+2^2+2+3^2+……+n^2 Here
Write a C program to find 1+1/2+1/3+1/4+....+1/n Here
Write a C program to find 1+1/2!+1/3!+1/4!+.....+1/n! Here
Write a C program to evaluate the series sum=1-x+x^2/2!-x^3/3!+x^4/4!......-x^9/9! Here
Write a C program to read and store the roll no and marks of 20 students using array Here
Write a C program to find out which number is even or odd from list of 10 number using array Here
Write a program to find maximum element from 1-Dimensional array Here
Write a C program to calculate the average, geometric and harmonic mean of n elements in a array Here
Write a program to delete a character in given string Here
Write a program to replace a character in given string Here
Write a program to find a character from given string Here
Write a program to sort given array in ascending order Here
Write a program to reverse string Here
Write a program to convert string into upper case Here
Write a program that defines a function to add first n numbers Here
Write a function in the program to return 1 if number is prime otherwise return 0 Here
Write a function Exchange to interchange the values of two variables, say x and y. illustrate the use of this function in a calling function Here
Write a C program to use recursive calls to evaluate F(x) = x – x3 / 3! + x5 / 5 ! – x7 / 7! + … xn/ n! Here
Write a program to find factorial of a number using recursion Here
Write a function that will scan a character string passed as an argument and convert all lowercase character into their uppercase equivalents Here
Write a program to read structure elements from keyboard Here

0 comments