program that computes the sum and the difference and the product of a range of consecutive numbers where the starting and ending numbers of the range are to be entered from the keyboard
#include <iostream>
using namespace std;
int main() {
int start, end;
cout << "Enter a start and an end of the region: ";
cin >> start >> end;
int sum=0, dif=0, prod=1;
for (int i=start; i<=end; i++) {
sum += i;
if (i == start) {
dif = start;
}
else {
dif -= i;
}
prod *= i;
}
cout << "The sum is " << sum << endl;
cout << "The difference is " << dif << endl;
cout << "The product is " << prod << endl;
return 0;
}
Comments
Leave a comment