I'm asked to re-write this program using the for loop.
Here is the original code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
int main()
{
int sum = 0, val = 1;
while (val <= 10) {
sum += val;
++val;
}
std::cout << "Sum of 1 to 10 inclusive is " << sum << std::endl;
system("pause");
return 0;
}
Here is what I wrote, using the for loop:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
int main()
{
int sum = 0;
for (int val = 1; val <= 10; ++val){
sum += val;
std::cout << "The sum of 1 to 10 inclusive is: " << sum << std::endl;
}
system("pause");
return 0;
}
I can't move out line 9 in the first code, it is the code I have to change, I can't change the first code, I had to re-write it, and if I remove line 9 in the 2nd code the program is basically not printing anything.