Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Read the coefficients a,b,c of the quadratic equation ax^2+bx+c and print it roots nicely for imaginary roots print in x+iy form

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

main() {
    int a, b, c, x, x1, x2;
    printf("enter the values of a,b,c:");
    scanf("%d%d%d", &a, &b, &c);
    printf("The quadratic equation is %d*pow(x,2)+%d*x+%d=0", a, b, c);

    if (pow(b, 2) - 4 * a * c >= 0) {
        x1 = (-b + sqrt(pow(b, 2) - 4 * a * c)) / 2 * a;
        x2 = (-b - sqrt(pow(b, 2) - 4 * a * c)) / 2 * a;
        printf("the roots of the equation are x1=%d,x2=%d", x1, x2);
    }
    else
        printf("roots of the equation in the form of x+iy and x-iy");

    return 0;
}

Is this code alright for the given question, i had a bit confusion at that printing imaginary roots. could you please help

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  1. Use proper main prototype.
  2. Use floating point numbers instead of integers
  3. pow(b,2) == b*b
  4. / 2 * a -> / (2 * a)
int main(void) {
    double a, b, c, x, x1, x2;
    printf("enter the values of a,b,c:");
    if(scanf("%lf %lf %lf", &a, &b, &c) != 3) { /* handle error */}
    printf("\nThe quadratic equation is %f*x^2+%f*x+%f=0\n", a, b, c);

    if (b*b - 4 * a * c >= 0) {
        x1 = (-b + sqrt(b*b - 4 * a * c)) / (2 * a);
        x2 = (-b - sqrt(b*b - 4 * a * c)) / (2 * a);
        printf("the roots of the equation are x1=%f,x2=%f\n", x1, x2);
    }
    else
        printf("roots of the equation in the form of x+iy and x-iy\n");
}

https://gcc.godbolt.org/z/93abv4obE

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading