1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Quadratic Equation

Discussion in 'C' started by meyup, Mar 1, 2010.

  1. meyup

    meyup New Member

    Joined:
    Feb 15, 2010
    Messages:
    102
    Likes Received:
    0
    Trophy Points:
    0
    Hi!
    While solving this program I got a problem, that is-
    ax^2 + bx + c = 0
    and this will be solved by [-b +/- root(b^2 - 4ac)]/2a
    now this equation will have two solutions, one is exat and another is imaginary. I'm having problem with imaginary portion, if thene is a negetive sign under the root i.r, root(b^2 - 4ac) the program will get error.
    What to do?:(
     
  2. pankaj.sea

    pankaj.sea New Member

    Joined:
    Apr 6, 2009
    Messages:
    461
    Likes Received:
    13
    Trophy Points:
    0
    Occupation:
    Web Developer
    Location:
    Kolkata
    Home Page:
    Will you please give your program code, so that we can modify it for you?
    :D
     
  3. pankaj.sea

    pankaj.sea New Member

    Joined:
    Apr 6, 2009
    Messages:
    461
    Likes Received:
    13
    Trophy Points:
    0
    Occupation:
    Web Developer
    Location:
    Kolkata
    Home Page:
    you can do one thing, in the root(b^2 - 4ac) section you will get two thing, one is real and another is imaginary, while calculating the root means
    Code:
    d=b*b-4*a*c;
    x=sqrt(d);
    after this code add a If-Else i.e,
    Code:
    if(d>0)
    x=sqrt(d);
    else
    {
    d2=-1*d;
    x=sqrt(d2);
    }
    Now while printing the value just add a "i" after the "%d" of imaginary part. i.e,
    Code:
    printf("%d i",&something);
    Hope this will solve your problem!
     
  4. meyup

    meyup New Member

    Joined:
    Feb 15, 2010
    Messages:
    102
    Likes Received:
    0
    Trophy Points:
    0
    Thanks Pankaj!
    after coding again with your suggestion I'll confirm or inform whatever! ;)
     
  5. meyup

    meyup New Member

    Joined:
    Feb 15, 2010
    Messages:
    102
    Likes Received:
    0
    Trophy Points:
    0
    Here is the code of the complete solution of the problem.
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    void main()
    {
    float a,b,c,d,x1,x2,y,z;
    clrscr();
    printf("Enter the value of a: ");
    scanf("%f",&a);
    printf("Enter the value of b:");
    scanf("%f",&b);
    printf("Enter the value of c:");
    scanf("%f",&c);
    y=b*b-4*a*c;
    if(y=>1)
    {
    d=sqrt(y);
    x1=(-b+d)/2*a;
    x2=(-b-d)/2*a;
    printf("The solutions are\nX1= %f and\nX2= %f",x1,x2);
    }
    else
    {
    z=(-1)*y;
    d=sqrt(z);
    x1=(-b+d)/2*a;
    x2=(-b-d)/2*a;
    printf("The solutions are\nX1= %fi and\nX2= %fi",x1,x2);
    }
    getch();
    }
    
    thanks...
     

Share This Page