Write a C program to calculate a Quadratic equation.

Function Main
    Declare Integer A
    Declare Integer B
    Declare Integer C
    Declare Integer Delta
    Declare Real X1
    Declare Real X2
    Output "Enter coefficient of X^2:"
    Input A
    Output "Enter coefficient of X:"
    Input B
    Output "Enter known term:"
    Input C
    If A = 0
        Output "Equarion:"
        Assign X1 = -C / B
        Output "The solution is X = " & X1
    Else
        Assign Delta = B^2 - 4*A*C
        If Delta < 0
            Output "There are no real solutions"
        Else
            If Delta = 0
                Assign X1 = -B / (2*A)
                Output "Two real and coincident solutions"
                Output "X1 = " & X1
                Output "X2 = " & X1
            Else
                Assign X1 = -B - SQRT(Delta) / (2*A)
                Assign X2 = -B + SQRT(Delta) / (2*A)
                Output "Two real and distinct solutions"
                Output "X1 = " & X1
                Output "X2 = " & X2
            End
        End
    End
End

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

int main() {
    int A;
    int B;
    int C;
    int Delta;
    double X1;
    double X2;
    printf("Enter coefficient of X^2:\n");
    scanf("%d", &A);
    printf("Enter coefficient of X:\n");
    scanf("%d", &B);
    printf("Enter known term:\n");
    scanf("%d", &C);
    if (A== 0) {
        printf("Equation:\n");
        X1 = -C / (double)B;
        printf("The solution is X = %lf\n", X1);
    } else {
        Delta = (int)(pow(B, 2) - 4 * A * C);
        if (Delta < 0) {
            printf("There are no real solutions\n");
        } else {
            if (Delta == 0) {
                X1 = -B / (2 * (double)A);
                printf("Two real and coincident solutions\n");
                printf("X1 = %lf\n", X1);
                printf("X2 = %lf\n", X1);
            } else {
                X1 = (-B - sqrt(Delta)) / (2 * (double)A);
                X2 = (-B + sqrt(Delta)) / (2 * (double)A);
                printf("Two real and distinct solutions\n");
                printf("X1 = %lf\n", X1);
                printf("X2 = %lf\n", X2);
            }
        }
    }
    return 0;
}

Enter coefficient of X^2:
5
Enter coefficient of X:
10
Enter known term:
5
Two real and coincident solutions
X1 = -1.000000
X2 = -1.000000

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.