EG2069
An example program, roots.c

This is a variation of the program to calculate the roots of a quadratic equation. This version has a central while() loop which repeats the calculation until a 0 is entered as the value for the quadratic coefficient.

Please note that care is taken to avoid illegal operations in this program, such as division by 0 and taking the square root of a negative number. It is important to test that the values of the variables used in an arithmetic expression are valid. Every time you enter a division or a square root (or any other operation with a limited range of permitted values) you should include a check for illegal values.


/* Program to calculate the roots of a quadratic equation */

#include <stdio.h>  /* printf() and scanf() */
#include <math.h>   /* sqrt() */

main()
{
   double aa, bb, cc, aux, root1, root2;

   aa = 1.0;
    
   printf("\nProgram to find the roots of a quadratic equation\n");
   printf("of the form a*x*x + b*x + c = 0\n\n");
   printf("Entering 0 for the value of a stops the program\n\n");

   while ( aa != 0.0 )
   {
      /* Enter coefficients for equation */   
      printf("Enter value for a : ");
      scanf("%lf", &aa);
      if ( aa == 0.0 )
      {
         /* Exit the while() loop, unless we want a division by 0 */
         break;
      }
      printf("Enter value for b : ");
      scanf("%lf", &bb);
      printf("Enter value for c : ");
      scanf("%lf", &cc);

      /* Better make sure there is a real root for these coefficients */     
      aux = bb * bb - 4 * aa * cc;
      
      if ( aux > 0.0 ) /* There are 2 solutions */
      {
          aux = sqrt( aux );    /* Our 'aux' variable now holds the square root
	                           of its previous value, we do this because
				   we don't want to calculate the square root 
                                   more than once */ 
          /* We have already made sure that aa !=0, so a division is safe */
          root1 = ( -bb + aux ) / ( 2 * aa );
          root2 = ( -bb - aux ) / ( 2 * aa );
          printf("\nThe roots %.4lf and %.4lf were found\n\n", root1, root2 );
      }
      else if ( aux == 0.0 ) /* There is only one solution */
      {
          root1 = -bb / ( 2 * aa );
          printf("\nA single root, %.4lf, was found\n\n", root1 );
      }
      else /* There is no real solution */
      {
          printf("\nNo real roots were found\n\n");
      }
   } /* End while() loop */
   
   printf("\nEnd of program, goodbye!\n\n");
 
   return;              
}

A sample run of this program is shown below.

gabor-7 $ cd cprog
gabor-8 $ ls -l
total 82
-rw-------   1 eng529     eng            125 Nov 10 15:12 first.c
-rw-------   1 eng529     eng            127 Nov 10 15:12 program1.c
-rwx--x--x   1 eng529     eng          37012 Nov 10 15:25 roots
-rw-------   1 eng529     eng           1828 Nov 10 15:12 roots.c        
gabor-9 $ roots

Program to find the roots of a quadratic equation
of the form a*x*x + b*x + c = 0

Entering 0 for the value of a stops the program

Enter value for a : 1.3
Enter value for b : -3.3
Enter value for c : 2.1

No real roots were found

Enter value for a : 1.3
Enter value for b : -3.3
Enter value for c : 0.7

The roots 2.3048 and 0.2336 were found

Enter value for a : 1
Enter value for b : 2
Enter value for c : 1

A single root, -1.0000, was found
         
Enter value for a : 0

End of program, goodbye!

gabor-10 $  

Back to main page
Author : Keith Halewood / Helge Nareid Current contact: Gorry Fairhurst G.Fairhurst@eng.abdn.ac.uk