1

This is the code I tried to get real and complex value for this equation, ax^2 + bx + c = 0.

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

int main() {
    double a, b, c, d, x1, x2, i = sqrt(-1);
    scanf("%lf %lf %lf", &a, &b, &c);
    d = (b * b) - (4 * a * c);
    x1 = (-b + sqrt(d)) / (a + a);
    x2 = (-b - sqrt(d)) / (a + a);
    if (d < 0) {
        printf("Complex solution\n");
        printf("R1 = %.2lf\n", x1);
        printf("R2 = %.2lf\n", x2);
    } else
    if (a == 0)
        printf("Value of a must be non zero\n");
    else {
        printf("R1 = %.2lf\n", x1);
        printf("R2 = %.2lf\n", x2);
    }
    return 0;
}

And I want to get an answer with i (eg: -.4 + 4i).

chqrlie
  • 131,814
  • 10
  • 121
  • 189
Maria
  • 67
  • 1
  • 3
  • 7

5 Answers5

4

Use <complex.h>

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

int cprint(const char *pre, complex double x, const char *post) {
    int n = printf("%s", pre);
    n += printf("%.2f", creal(x));
    if (cimag(x)) n += printf(" %+.2fi", cimag(x));
    n += printf("%s", post);
    return n;
}

int main(void) {
    double a, b, c, d;
    complex double x1, x2;
    if (scanf("%lf%lf%lf", &a, &b, &c) != 3) {
        printf("Bad input\n");
        exit(EXIT_FAILURE);
    }
    if (a == 0) {
        printf("Value of a must be non zero\n");
        exit(EXIT_FAILURE);
    }
    d = (b * b) - (4 * a * c);
    x1 = (-b + csqrt(d)) / (a + a); // using csqrt()
    x2 = (-b - csqrt(d)) / (a + a); // complex square root
    if (d < 0) {
        printf("Complex solution\n");
    }
    cprint("R1 = ", x1, "\n");
    cprint("R2 = ", x2, "\n");
    return 0;
}
pmg
  • 106,608
  • 13
  • 126
  • 198
  • Thanks for your approach, Sir. But I am a beginner. Most of the functions used in your code are unknown to me. – Maria Jul 15 '20 at 14:44
  • 3
    The only new function I've used is `csqrt()`: it calculates the square root of complex numbers, returning a complex number. Other than that I've used complex numbers (and ``) which you can think of as an (integrated) array of 2 double values, one for the real part and another for the imaginary part. – pmg Jul 15 '20 at 15:01
  • Okay. Now I got it. – Maria Jul 17 '20 at 06:46
2

You can calculate the complex part yourself:

#include<stdio.h>
#include<math.h>
int main()
{
    double a,b,c;
    scanf("%lf %lf %lf",&a,&b,&c);
    if (a==0)
    {
        printf("Value of a must be non zero\n");
        return 0;
    }
    double d = (b*b)-(4*a*c);
 
    if (d<0){
        double imag = sqrt(-d)/(a+a);
        double real = -b/(a+a);
        printf("Complex solution\n");
        printf("R1 = %.2f + i * %.2f\n",real, imag);
        printf("R2 = %.2f - i * %.2f\n",real, imag);
 
    }
    else
    {
        double x1 = (-b + sqrt(d))/(a+a);
        double x2 = (-b - sqrt(d))/(a+a);
        printf("R1 = %.2f\n",x1);
        printf("R2 = %.2f\n",x2);
    }
    return 0;
}

https://ideone.com/R7UwhG

mch
  • 9,424
  • 2
  • 28
  • 42
1

When you declare a variable as double, you declare a real number, not a complex one. For declaring and using complex numbers, use the library complex.h as described in How to work with complex numbers in C?.

Pierre François
  • 5,850
  • 1
  • 17
  • 38
  • Thanks a lot for your approach, Sir. As I am beginner. So, complex.h library function is unknown to me. – Maria Jul 15 '20 at 14:49
1

well, let's talk a little bit about quadratic equation :

The term b2-4ac is known as the discriminant of a quadratic equation. The discriminant tells the nature of the roots.

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

i guess if we follow the math, you can write a smooth code for that.

but for a small hint, you can find more explanation about quadratic equation and how to implement a C program to solve it here : https://www.programiz.com/cpp-programming/examples/quadratic-roots :) .

walid barakat
  • 455
  • 1
  • 6
  • 17
1

Your program has multiple issues:

  • sqrt() is a real function, -1 is outside its domain.
  • you can use complex numbers if the compiler supports them by defining x1 and x2 as complex double x1, x2;. But it is just as easy to compute the complex components yourself.
  • you should check the return value of scanf() to detect invalid input.
  • if a == 0 the equation becomes a first degree equation, that may or may not have solutions.

Here is a modified version:

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

int main() {
    double a, b, c, d;

    if (scanf("%lf %lf %lf", &a, &b, &c) != 3) {
        printf("Invalid input\n");
        return 1;
    }
    if (a == 0) {
        printf("Value of a must be non zero\n");

        if (b == 0) {
            if (c == 0) {
                printf("Equality is true for all values of x\n");
            } else {
                printf("No solution\n");
            }
        } else {
            double x = (-c) / b;

            printf("Single solution\n");
            printf("R1 = %.2f\n", x);
        }
        return 0;
    }
    d = (b * b) - (4 * a * c);
    if (d < 0) {
        double x = (-b) / (a + a);
        double y = fabs(sqrt(-d) / (a + a));
        
        printf("Complex solutions\n");
        printf("R1 = %.2f - %.2fi\n", x, y);
        printf("R2 = %.2f + %.2fi\n", x, y);
    } else
    if (d == 0) {
        double x = (-b) / (a + a);

        printf("Single real solution\n");
        printf("R1 = %.2f\n", x);
    } else {
        double x1 = (-b + sqrt(d)) / (a + a);
        double x2 = (-b - sqrt(d)) / (a + a);

        printf("Real solutions\n");
        printf("R1 = %.2f\n", x1);
        printf("R2 = %.2f\n", x2);
    }
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189