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

  1. // Program calculates a sum and average of a range of 
  2. // integers using a for loop
  3.  
  4. #include <iostream.h>
  5.  
  6. main()
  7. {
  8.     double sum = 0;
  9.     double sumx = 0.0;
  10.     int first, last, temp;
  11.  
  12.     cout << "Enter the first integer : ";
  13.     cin >> first;
  14.     cout << "Enter the last integer : ";
  15.     cin >> last;         
  16.     if (first > last) {
  17.       temp= first;
  18.       first = last;
  19.       last = temp;
  20.     }
  21.     for (int i = first; i <= last; i++) {
  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. }
  31.