Q:

We can use a while to write a program to sum the numbers from 1 through 10 inclusive as follows

0

We can use a while to write a program to sum the numbers from 1 through 10 inclusive as follows.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <iostream>
int main()
{
int sum = 0, val = 1;
// keep executing the while as long as val is less than or equal to 10
while (val <= 10) {
sum += val; // assigns sum + val to sum
++val; // add 1 to val
}
std::cout << "Sum of 1 to 10 inclusive is "<< sum << std::endl;
return 0;
}

 

Output:

Sum of 1 to 10 inclusive is 55

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now