Q:

C program to find the roots of a quadratic equation

0

C program to find the roots of a quadratic equation

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Read the value of ab, and c, and calculate the roots of a quadratic equation.

Program:

The source code to roots of a quadratic equation is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.

//C program to roots of a quadratic equation

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void calculateRoot(float a, float b, float c)
{
    float rootA = 0;
    float rootB = 0;

    float realp = 0;
    float imagp = 0;
    float disc = 0;

    if (a == 0 || b == 0 || c == 0) {
        printf("Error: Unable to determine roots\n");
        return;
    }
    else {
        disc = b * b - 4.0 * a * c;
        if (disc < 0) {
            printf("Imaginary Roots\n");
            realp = -b / (2.0 * a);
            imagp = sqrt(abs(disc)) / (2.0 * a);
            printf("Root1 = %f  +i %f\n", realp, imagp);
            printf("Root2 = %f  -i %f\n", realp, imagp);
        }
        else if (disc > 0) {
            printf("Roots are real and distinct \n");
            rootA = (-b + sqrt(disc)) / (2.0 * a);
            rootB = (-b - sqrt(disc)) / (2.0 * a);
            printf("Root1 = %f  \n", rootA);
            printf("Root2 = %f  \n", rootB);
        }
        else if (disc == 0) {
            printf("Roots are real and equal\n");
            rootA = -b / (2.0 * a);
            rootB = rootA;
            printf("Root1 = %f\n", rootA);
            printf("Root2 = %f\n", rootB);
        }
    }
}

int main()
{
    float a = 0;
    float b = 0;
    float c = 0;

    printf("Enter the values of a: ");
    scanf("%f", &a);

    printf("Enter the values of b: ");
    scanf("%f", &b);

    printf("Enter the values of c: ");
    scanf("%f", &c);

    calculateRoot(a, b, c);

    return 0;
}

Output:

RUN 1:
Enter the values of a: 20
Enter the values of b: 30
Enter the values of c: 40
Imaginary Roots
Root1 = -0.750000  +i 1.198958
Root2 = -0.750000  -i 1.198958

RUN 2:
Enter the values of a: 1.2
Enter the values of b: 2.3
Enter the values of c: 3.4
Imaginary Roots
Root1 = -0.958333  +i 1.381927
Root2 = -0.958333  -i 1.381927

RUN 3:
Enter the values of a: 0
Enter the values of b: 0
Enter the values of c: 1
Error: Unable to determine roots

Explanation:

In the above program, we created two functions calculateRoot() and main(). The calculateRoot() function is used to find-out the roots of a Quadratic Equation.

In the main() functiom, we read three integer numbers abc from the user and called the calculateRoot() function to find-out the roots of a Quadratic Equation and printed the result on the console screen.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now