Untitled

mail@pastecode.io avatar
unknown
c_cpp
8 months ago
2.9 kB
2
Indexable
Never
#define _CRT_SECURE_NO_WARNINGS

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

int main(void) {

    int a, b, c, delta, absdelta;
    float sdelta, x1, x2, re1, re2, im1, im2; // creating a variables

    printf("This program will solve the quadratic equation of the form: "); // intro
    printf("ax^2+bx+c=0\n");

    printf("\nEnter the value of a here: "); // asking for the value of a
    scanf("%d", &a);
    printf("\nEnter the value of b here: "); // asking for the value of b
    scanf("%d", &b);
    printf("\nEnter the value of c here: "); // asking for the value of c
    scanf("%d", &c);

    if (a == 0) {
        if (b == 0) { // linear equation
            if (c == 0) {
                printf("\nx can be any number");
            }
            else {
                printf("\nthere is no solution");
            }
        }
        else {
            printf("\nx = %f\n", -(float)c / b);
        }
    }
    else {
        if (b == 0) {
            if (a * c > 0) {
                printf("\nThere are no real solutions\n");
            }
            else {
                float sq;
                sq = sqrt(-(float)c / a);
                printf("\n x1 = %f\n", sq);
                printf("\n x2 = %f", -1*sq);
            }
        }
        else {
            if (c == 0) {
                printf("\nx1 = 0\n");
                printf("x2 = %f", -(float)b / a);
            }

            else {

                delta = b * b - 4 * a * c; // computing the discriminant
                sdelta = sqrt((float)delta); // computing square root of discriminant

                if (delta > 0) { // checking the value of discriminant (>0)

                    x1 = ((-(float)b + sdelta) / (2 * (float)a)); // computing the first root
                    x2 = ((-(float)b - sdelta) / (2 * (float)a)); // computing the second root

                    printf("The roots are: %f and %f\n", x1, x2); // printing the roots
                }

                if (delta == 0) { // checking the value of discriminant (=0)

                    x1 = ((-(float)b + sqrt((float)delta)) / (2 * (float)a)); // computing the root

                    printf("The root is: %f\n", x1); // printing the root
                }

                if (delta < 0) { // checking the value of discriminant (<0)

                    printf("\nThere are no real solutions\n");

                    //absdelta = delta * (-1); // computing the absolute value of diccriminant

                    //re1 = (-(float)b / (2 * (float)a)); // computing the real parts of the roots
                    //re2 = (-(float)b / (2 * (float)a));

                    //im1 = (sdelta / (2 * (float)a)); // computing the imaginary parts of the roots
                    //im2 = (-sdelta / (2 * (float)a));

                    //printf("\nThe roots are: %f + %fi and %f + %fi", re1, re2, im1, im2); // printing the roots
                }
            }
        }
    }
}
Leave a Comment