Sum of numbers

Hello,

Welcome back!

This time I will show you a great code using LOOP. In the last WSQ I explained what is a loop and now we are goint to use it again.

Instructions:

Write a program that asks for a range of integers and then prints the sum of the numbers in that range (inclusive).

You can use a formula to calculate this of course but what we want you to do here is practice using a loop to do repetitive work.

And here’s the code:

2016-02-06

The same as the last one but in this code we are introducing a new tool what I call “counter” and “accumulator”. But, what is the function of each one?

Counter: we can use it by our benefit abd it depends in what kind code we are trying to make. Most of cases it helps us to count how many times the cycle is repeated and to use it in the condition.

Accumulator: it is not difficult to think what an accumulator is. This variable accumulate all the values that are generated by the cycle. Most of the time the value changes.

As you can see I defined the counter as “num”. Remember to declare the value of the counter as cero and add “+ 1” and with every cycle the counter is goin to plus one round.

It is not difficult just need to practice and think logically.

Code’s link: here

I remember how to do it because I used in my last course and to see if it is the same I visit this web and be sure.

See you in my next post!

Top image by: https://www.flickr.com/photos/joeyz51/9323915464/sizes/l/

CC BY-SA 4.0 Sum of numbers by sercho93 is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.