Basic Programming C Examples

WAP in C to calculate the real roots of the quadratic equation

The Program in C that calculates the real roots of the quadratic equation is given below:

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

int main() {
    float a, b, c, discriminant, root1, root2;

    printf("Enter the values of a, b and c: ");
    scanf("%f %f %f", &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("The roots are real and different.\n");
        printf("root1 = %.2f and root2 = %.2f", root1, root2);
    }

    //condition for real and same roots
    else if (discriminant == 0) {
        root1 = root2 = -b / (2*a);
        printf("The roots are real and same.\n");
        printf("root1 = root2 = %.2f;", root1);
    }

    //if roots are not real
    else {
        printf("The roots are complex and different.");
    }

    return 0;
}

Output:

Enter the values of a, b, and c: 23
45
99
The roots are complex and different.

Pro-Tips💡

This program prompts the user to enter the values of the coefficients (a, b, and c) of the quadratic equation (ax^2 + bx + c = 0).

Then it uses the formula to calculate the discriminant (b^2 – 4ac) to check if the roots are real or complex.

If the discriminant is greater than 0, the roots are real and different, if the discriminant is equal to 0 the roots are real and the same, and if the discriminant is less than 0, the roots are complex and different.

The program then uses the appropriate formula to calculate the roots and prints them out.

Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

Programming Languages( Types, Pros and Cons)-Codeauri

What are the types or levels of Programming Languages? The types or levels of programming languages are divided into two types: Types Of Programming Languages: 1.Machine-level Language :1st…

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X