Take a number N as input and output the sum of all numbers from 1 to N (including N). | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Take a number N as input and output the sum of all numbers from 1 to N (including N).

````````

3rd Jul 2022, 9:22 PM
VISHAL CHAUDHARY
VISHAL CHAUDHARY - avatar
2 Answers
+ 3
Jay Matthews , i am quite sure that this exercise is not meant to be solved by using a formula 🙃. i guess that using a loop does help (more) for learning and growing in experience. -> use input() function to get the max number for building the sum. store it in a variable. data format has to be integer -> use a variable that can hold the running total -> use a for loop and iterate through a range() using the required arguments. (keep in mind that the max value will not be included in the range) add the values getting from loop to the total variable -> after the loop is done, print the number from total
4th Jul 2022, 2:52 PM
Lothar
Lothar - avatar
+ 1
Jay Matthews /2 lol thought JM meant something else in his response to my brief question "/2"(thanks) I am kind of afraid of asking how //2 is same as >>, looking it up, "the bitwise right shift operator" seems 😅 edit (about >>) : https://stackoverflow.com/questions/37535864/what-does-mean-in-JUMP_LINK__&&__python__&&__JUMP_LINK https://stackoverflow.com/questions/54047100/why-are-bitwise-operators-slower-than-multiplication-division-modulo https://en.m.wikipedia.org/wiki/Arbitrary-precision_arithmetic
3rd Jul 2022, 10:01 PM
Korkunç el Gato
Korkunç el Gato - avatar