Repeat Prob. 6.45 calculating the sum of every nth integer, beginning with the value assigned to nstart (i.e., for
i= nstart, nstart + n, nstart + 2*n, nstart + 3*n, etc.).
#include <iostream>
using namespace std;
int main() {
int nstart, nend, n;
cout << "Enter nstart: ";
cin >> nstart;
cout << "Enter nend: ";
cin >> nend;
cout << "Enter n: ";
cin >> n;
int sum=0;
for (int i=nstart; i<=nend; i+=n) {
sum += i;
}
cout << "The sum is " << sum;
return 0;
}
Comments
Leave a comment