Thread: while loops...a problem with my simple code?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    12

    while loops...a problem with my simple code?

    I think there may be a problem with my usage of brackets below.
    Any ideas on this simple program to find roots of quadratic equation?

    many thanks!

    include
    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <math.h> 
    int main()
    
    {
    char answer;
    int a,b,c;
    float single_root, root_part1, root_part2;//declaring function variables these 
                                              //being different elements of the 
                                              //quadratic formula
    float root1, root2;
    int is_complex;
    
    do
    
    printf("Enter coeficients a b c: ");
    scanf("&#37;f %f %f", &a, &b, &c);
    
    is_complex = 0;   //(is_complex) function = 0 if a=0
       
       if (a==0)
       {
       single_root=-1.0*c/b;//stating variables
       }
       
       else
       
       {
       root_part1 = -1.0*b/(2.0*a); //stating variables
       root_part2= b*b-4.0*a*c;
       
          if (root_part2<0.0)
          {
                             
          is_complex = 1; 
          
          root_part2= -1.0*root_part2;//remember this is not equal to, 
                                      //this is a directive
          }
          
                
           root_part2=sqrt(root_part2)/(2.0*a);
                
           }
                
            if(a==0.0)
                
           {
            Printf("single_root = %f\n ", single_root);//single root case
           }
                
            else if  (is_complex==1)
                   
           {
           printf("root 1 = %.2f+%.2f i\n", root_part1, root_part2); 
           printf("root 1 = %.2f-%.2f i\n", root_part1, root_part2);
           }
                   
           else 
                   
           {
           root1= root_part1+root_part2;  
           root2= root_part1-root_part2;
                   
       printf("root1 = %.2f", root1);
       printf("root2 = %.2f", root2);
       }
                   
       {
       printf("enter next set of coefficients (y/n): ");
       scanf(" %c", &answer);
       }
    
               
       while (answer != 'n' && answer != 'N'); 
               
       scanf("d\n");   
       exit(0);
       
    }
    Last edited by niceguy; 02-20-2008 at 02:18 PM. Reason: My eyes tire of the wide view.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  2. Odd problem in main(), code won't proceed
    By aciarlillo in forum C++ Programming
    Replies: 1
    Last Post: 06-05-2005, 11:00 PM
  3. Problem using java programs within C code
    By lemania in forum Linux Programming
    Replies: 1
    Last Post: 05-08-2005, 02:02 AM
  4. Simple Code, looking for input.
    By Alien_Freak in forum C Programming
    Replies: 3
    Last Post: 03-03-2002, 11:34 AM
  5. Simple Compile Time Problem - HELP!
    By kamikazeecows in forum Windows Programming
    Replies: 2
    Last Post: 12-02-2001, 01:30 PM