home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C Programming Starter Kit 2.0
/
SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso
/
tybc4
/
for1.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1993-03-22
|
708b
|
31 lines
// Program calculates a sum and average of a range of
// integers using a for loop
#include <iostream.h>
main()
{
double sum = 0;
double sumx = 0.0;
int first, last, temp;
cout << "Enter the first integer : ";
cin >> first;
cout << "Enter the last integer : ";
cin >> last;
if (first > last) {
temp= first;
first = last;
last = temp;
}
for (int i = first; i <= last; i++) {
sum++;
sumx += (double)i;
}
cout << "Sum of integers from "
<< first << " to " << last << " = "
<< sumx << "\n";
cout << "Average value = " << sumx / sum;
return 0;
}