Thread: quadratic equation solving

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    45

    quadratic equation solving

    i use codeblocks for compiling.
    The following is the code that i have written for solving quadratic equation.

    Code:
    #include<math.h>
    
    
    main()
    {
    
        float a,b,c,discriminant,root1,root2;
        printf("input values of a,b, and c\n");
        scanf("%f %f %f",&a,&b,&c);
        discriminant = (b*b - 4*a*c);
        if (discriminant<0)
           printf("\n\nROOTS ARE IMAGINARY\n");
        else
         {
             root1=(-b+sqrt(discriminant))*1/(2.0*a);
             root2=(-b-sqrt(discriminant))*1/(2.0*a);
             printf("\n\nRoot1=%f\n\nRoot2=%f\n,root1,root2");
    
         }
    
    }
    but when i enter the numbers 2,4,-16
    it shows
    root1=0.000000
    root2=0.000000

    i dont know what is wrong can anybody please help
    Last edited by narendrav; 05-18-2011 at 08:41 AM. Reason: making it short

  2. #2
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    Code:
    printf("\n\nRoot1=%f\n\nRoot2=%f\n,root1,root2");
    Do you see anything wrong with this statement? Pay attention to the warnings your compiler emits. Also, include <stdio.h>
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Include stdio.h which include prototypes of printf,scanf,...
    main function signature should be int main(void) and return 0 at the end.
    Code:
    int main(void)
    {
       ....
     return 0;    // success
    }
    Code:
             printf("\n\nRoot1=%f\n\nRoot2=%f\n,root1,root2");
             printf( format_string,  ....);
      eg. printf("int %d\n", 3);
             printf("root = %f\n",root);
    Last edited by Bayint Naung; 05-18-2011 at 08:43 AM.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    45
    what is the error with printf statement.can you please tell

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    45
    thank you @Bayint Naung.It was foolish of me to write that line incorrectly.I dont know why the compiler did not point out.

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    It's pointing you out 'saying not enough arguments..... because %f %f in your format string but no args.'

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    45
    yes i got it now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quadratic equation
    By nynicue in forum C Programming
    Replies: 5
    Last Post: 12-26-2008, 05:57 PM
  2. quadratic equation cpp program problems
    By faluiretoread in forum C++ Programming
    Replies: 2
    Last Post: 11-09-2008, 03:22 PM
  3. Quadratic Equation
    By Lucid15 in forum C Programming
    Replies: 20
    Last Post: 09-16-2008, 05:59 PM
  4. Quadratic Equation problems
    By Rachel228 in forum C++ Programming
    Replies: 2
    Last Post: 03-15-2006, 02:42 AM
  5. Quadratic Equation Program
    By Ambizzy in forum C Programming
    Replies: 4
    Last Post: 02-19-2002, 09:21 PM