home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / tybc4 / while1.cpp < prev    next >
C/C++ Source or Header  |  1993-03-22  |  690b  |  30 lines

  1. // Program demonstrates the do-while loop
  2.  
  3. #include <iostream.h>
  4.  
  5. main()
  6. {
  7.     double sum = 0;
  8.     double sumx = 0.0;
  9.     int first, last, temp, i;
  10.  
  11.     cout << "Enter the first integer : ";
  12.     cin >> first;
  13.     cout << "Enter the last integer : ";
  14.     cin >> last;         
  15.     if (first > last) {
  16.       temp= first;
  17.       first = last;
  18.       last = temp;
  19.     }                       
  20.     i = first;
  21.     while (i <= last) {
  22.       sum++;
  23.       sumx += (double)i++;
  24.     }                   
  25.     cout << "Sum of integers from " 
  26.          << first << " to " << last << " = "
  27.          << sumx << "\n";
  28.     cout << "Average value = " << sumx / sum;
  29.     return 0;
  30. }