WSQ07
SUM of numbers
Va el primero:
Se declara las variables primero:
Comments Off on WSQ07 SUM OF NUMBERS
Posted in Blogs, Syndicated, TC1014 Blogs
Va el primero:
Se declara las variables primero:
Comments Off on WSQ07 SUM OF NUMBERS
Posted in Blogs, Syndicated, TC1014 Blogs
Here is my #wsq07 which is about a prgram that sum the numbers in a given range, for example from 1 to 4 will sum, 1+2+3+4=10.
Here is my source code and the program running.
Enjoy!
#WSQ07 on this code the program make the sum of two numbers given by the user,
for example this program asked the user for two numbers. in this case 5 and 7; and then the program would sum 5+6+7=18
#WSQ07 on this code the program make the sum of two numbers given by the user,
for example this program asked the user for two numbers. in this case 5 and 7; and then the program would sum 5+6+7=18
For #WSQ07 of my #TC1017 course I programmed a very interesting program to understand the use of “for” loops, and to understand the increment operators and compound assignment operators:
The program sums every number in a range given by the user.
Lets say the user gave 1 as the first number and 10 as the second number. The program would do the following:
0+1+2+3+4+5+6+7+8+9+10=55
The program also compares the first and the last numbers and makes reacomodates the first below the last so that the range is correctly calculated.
I´ll post a picture of my code.
Here notice im using a “For” loop.
First i assing “counter” the value of “first”. This only happens when the loop initializes. Then, For “Counter” is less or equal than “last”, counter is incremented by one. And if the conditional is true, the code inside the loop runs.
Sum += counter
Which can also be written as: Sum = Sum + counter.
When the conditional is no longer true, the program exits the loop and prints the value of “sum”.
here it is how it looks on the terminal.
Cheers.