previous | start | next

Example 4



    1   // File: max.cpp
    2   // Description: Computes and prints the maximum of two numbers
    3   // Author: 
    4   // Created: 07 Sep 05
    5   
    6   #include <iostream>
    7   #include <cmath>
    8   
    9   using namespace std;
   10   
   11   int main()
   12   {
   13     double x = 1403.0;
   14   
   15     double a, b, max;
   16   
   17     a = x/2.0;
   18     b = sqrt(x); // Square root; sqrt is declared in header file cmath
   19     cout.setf(ios::fixed);
   20     cout.precision(4);
   21     
   22     if ( a < b) {
   23       max = b;
   24     } else {
   25       max = a;
   26     }
   27   
   28     cout << "x = " << x << endl;
   29     cout << "x/2 = " << a << endl;
   30     cout << "sqrt(x) = " << b << endl;
   31   
   32     if ( a < b) {
   33       cout << "The maximum of x/2 and sqrt(x) is sqrt(x)" << endl;
   34     } else {
   35       cout << "The maximum of x/2 and sqrt(x) is x/2"  << endl;
   36     }
   37   
   38     return 0;
   39     
   40   }

Output:
x = 1403.0000
x/2 = 701.5000
sqrt(x) = 37.4566
The maximum of x/2 and sqrt(x) is x/2



previous | start | next