WSQ07 ‘Sum of Numbers’

Ever wondered what is the result of the addition of all the intergers from a certain range? Of course not, but let’s calculate it!

okami doge
Sorry, been playing Okami lately. 

What I mean by ‘Sum of Numbers’, it is for example: from a range of 5 to 10 one has to sum 5 + 6+ 7 + 8 + 9 + 10. This has to be done in order to get the final result of 45.  Sounds easy right? But what if the range was bigger? For example from 1 to 100. One is not going to grab a calculator and start “1+2+3+4+5+6+7+8+9+(…)+100”. Of course not! This is why I was in charge of programming a code were the computer automatically does this.

 

 

I will be using the classic variables “x” and “y” to define the range. This time “x” is the number we begin, and “y” is the number the addition countdown stops. However, I had to make a prediction were the user actually submits “x” higher than “y”. This would me impossible since the program is unable to count backwards. So I used a conditional “If” to let the user know what the mistake is. In the end, this is my code.

WSQ07 code

Looks cool right? It looks better when one uses it.

wsq07run

This blog corresponds to the song “After Midnight” by Blin182. I am not that old, yet I know that band brings nostalgia to most of the 90’s kids. This song was recommended by an old friend of mine, and I have must say thanks to her for introducing me this awesome song.

After Midnight

 

CC BY-SA 4.0 WSQ07 ‘Sum of Numbers’ by rhcpalmarichie is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.