Home > Software design >  How to print the imaginary root of quadratic equation in c?
How to print the imaginary root of quadratic equation in c?

Time:12-08

I need to print the imaginary root in the quadratic equation. but when I execute my code the result shows me that the imaginary root is 0.00000i. even I use to <complex.h> also same.

Can everybody help me to check the code that I bold?

//C program to find the root of the quadratic equation
#include<stdio.h>
#include<math.h>
#include<complex.h>

int main()
{
    double  a, b, c, x, x1, x2, disc, xr, ximg1, ximg2;
    
    printf("Please enter the value of quadratic equation, a: ");
    scanf("%lf", &a);
    printf("Please enter the value of quadratic equation, b: ");
    scanf("%lf", &b);
    printf("Please enter the value of quadratic equation, c: ");
    scanf("%lf", &c);

    if( a == 0 )
    {
        x = -(c/b);
        printf("\nThis is not a quadratic equation.\n");
        printf("x = %.3lf", x);
    }
    else{
    disc = (b*b) - 4*a*c;
        if( disc == 0 ){
             x1 = -b / 2 * a;
             x2 = -b / 2 * a;
             printf("x1 = %lf, x2 = %lf", x1, x2);
        }
        else if(disc > 0){
            x1 = ( -b   sqrt( disc ) ) / 2 * a;
            x2 = ( -b - sqrt( disc ) ) / 2 * a;
            printf("x1 = %.1lf, x2 = %.1lf", x1, x2);
        }
        else{
            ximg1 = sqrt( disc ) / 2 * a;
            ximg2 = - sqrt( disc ) / 2 * a;
            xr = - b / ( 2 * a );
            **printf("xr = %lf, ximg1 = %lfi, ximg2 = %lfi", crealf(xr), cimagf(ximg1), cimagf(ximg2));**
        }
    }

    return 0;
}

The output are shown as below:

Please enter the value of quadratic equation, a: 4
Please enter the value of quadratic equation, b: 1
Please enter the value of quadratic equation, c: 3
xr = -0.125000, ximg1 = 0.000000i, ximg2 = 0.000000i
Process returned 0 (0x0)   execution time : 3.914 s
Press any key to continue.

CodePudding user response:

You should print the roots as complex numbers using the computed real and imaginary parts. No need for <complex.h> nor complex types:

    double xr = - b / ( 2 * a );
    double ximg1 = -sqrt( -disc ) / (2 * a);
    double ximg2 = sqrt( -disc ) / (2 * a);
    printf("x1 = %lf% lfi, x2 = %lf% lfi\n", xr, ximg1, xr, ximg2);

CodePudding user response:

As you have used complex header file you just need to use the csqrt() instead of sqrt(),

//C program to find the root of the quadratic equation
#include<stdio.h>
#include<math.h>
#include<complex.h>

int main()
{
    double  a, b, c, x, x1, x2, disc, xr, ximg1, ximg2;
    
    printf("Please enter the value of quadratic equation, a: ");
    scanf("%lf", &a);
    printf("Please enter the value of quadratic equation, b: ");
    scanf("%lf", &b);
    printf("Please enter the value of quadratic equation, c: ");
    scanf("%lf", &c);

    if( a == 0 )
    {
        x = -(c/b);
        printf("\nThis is not a quadratic equation.\n");
        printf("x = %.3lf", x);
    }
    else{
    disc = (b*b) - 4*a*c;
        if( disc == 0 ){
             x1 = -b / 2 * a;
             x2 = -b / 2 * a;
             printf("x1 = %lf, x2 = %lf", x1, x2);
        }
        else if(disc > 0){
            x1 = ( -b   csqrt( disc ) ) / 2 * a;//changed the function here
            x2 = ( -b - csqrt( disc ) ) / 2 * a;//changed the function here
            printf("x1 = %.1lf, x2 = %.1lf", x1, x2);
        }
        else{
            ximg1 = sqrt( disc ) / 2 * a;
            ximg2 = - sqrt( disc ) / 2 * a;
            xr = - b / ( 2 * a );
            **printf("xr = %lf, ximg1 = %lfi, ximg2 = %lfi", crealf(xr), cimagf(ximg1), cimagf(ximg2));**
        }
    }

    return 0;
}

CodePudding user response:

Complex types not needed

When code reaches the below, disc < 0. Find the square root of the negation.

        // ximg1 = sqrt( disc ) / 2 * a;
        ximg1 = sqrt( -disc ) / 2 * a;     // Use -disc
        // ximg2 = - sqrt( disc ) / 2 * a;
        ximg2 = -ximg1;                    // Simply negate ximg1
        xr = - b / ( 2 * a );
        printf("xr = %lf, ximg1 = %lfi, ximg2 = %lfi", 
        // crealf(xr), cimagf(ximg1), cimagf(ximg2));
          xr, ximg1, ximg2);

Tip: use "%e", it is more informative.

        printf("xr = %le, ximg1 = %lei, ximg2 = %lei", 
          xr, ximg1, ximg2);

Bug

Instead of / 2 * a;, in various places, certainly you want / (2 * a);.

  • Related