To sum the numbers from 50 to 10 using while

Hello, I'm really new to the C++ programming so this question might look silly or so. I need to write a program that uses a while to sum the numbers from 50 to 100.


This program is for excercise in the one C++ book I'm learning from.
It gave the following explained code, which I did understand, but not fully, I guess.

1
2
3
4
5
6
7
8
9
10
11
12
13
#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;
}


Can someone explain me why it prints "Sum of 1 to 10 inclusive is 55" and could give a hint or so how to make program like this: Write a program that uses a while to sum the numbers from 50 to 100.

Thank you.
Try to work through the code.

1. Initially sum = 0, val = 1.
2. since (val <= 10), it enters the while loop.
3. sum = sum + val = 0 + 1 = 1.
4. val = val + 1 = 2.
5. val is still <= 10, so statements 3 and 4 are repeated.
6. sum = sum + val = 1 + 2 = 3.
7. val = val + 1 = 3.

You can see from the above iterations, that when the iterations end,
sum = 0 + 1 + 2 + 3 + .... + 10
val = 11.

For your next question, the hint is in the existing code itself.
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; //These are two int variables.
// keep executing the while as long as val is less than or equal to 10
    while (val <= 10) {
    sum += val; // the += means the same as sum = sum + val
    ++val; // add 1 to val and then repeat.
}
    std::cout << "Sum of 1 to 10 inclusive is " << sum << std::endl;
            
return 0;
} 




first we start with sum = 0 and add val(1) to it. now sum = 1. repeate.
sum = 1 and add val(2) to it. now sum = 3. repeat
sum = 3 and add val(3) to it. now sum = 6. repeat...
sum = 6 and add val(4) to it. now sum = 10. repeat :)
we keep doing this until val = 11. now we stop. do not add any more.
what is the end value of sum?

to solve your problem... set sum to start at 50. What do you think val should start at? and what should be the cut off value of val to stop the loop?
Maybe val should be 51 as it was 1 more before ?

The cut off value should be 100, then.

So should the program look to something like this ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
int main()
{
int sum = 50, val = 51; //These are two int variables.

    while (val <= 100) {
    sum += val;
    ++val;
}
    std::cout << "Sum of 50 to 100 inclusive is " << sum << std::endl;
            
return 0;
} 




yes your code is correct but your statement logic:

>Maybe val should be 51 as it was 1 more before ?

is not how you should think. sum already has a value of 50. So you should start adding 51 onwards. You should stop when the value of val exceeds 100 because you want to keep adding only till 100.
OK, I understand now. Thank you for the help. :) Appreciated.
closed account (EwCjE3v7)
Your reading c++ primer 5th edition..that's wat I'm reading ..I'm in chapter 2 also 1suggestion to all...read each chapter twice it will make more scense the second time
@CaptainBlastXD
also 1suggestion to all...read each chapter twice it will make more scense the second time


What to do if I never read this book neither one time nor two times?:)
Last edited on
@Brighton

Can someone explain me why it prints "Sum of 1 to 10 inclusive is 55"


It is well known from arithmetic that the sum of the sequence of integers 1, 2, ..., n is equal to ( n * ( n + 1 ) ) / 2.

So when n = 10 you get ( 10 * ( 10 + 1 ) ) / 2 = 55.

So before all learn arithmetic!:)
Topic archived. No new replies allowed.