- 1

I have to do this exercise in Python but I don't know how to do it:

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

16th Mar 2021, 1:20 PM
Riccardo Barbarossa
Riccardo Barbarossa - avatar
3 Antworten
+ 3
I know it's hard for beginner but try to start understand the Python concept , you can certainly solve it easily 😁 n = int(input("Input a number: ")) sum_all = (n * (n + 1)) / 2 print(sum_all)
16th Mar 2021, 1:30 PM
Ramadhani
Ramadhani - avatar
+ 3
You can find all kinds of solutions here: https://stackoverflow.com/questions/43901484/sum-of-the-integers-from-1-to-n However, the easiest way is to just use the formula: sum[1, n] = n * ( n + 1 ) / 2
16th Mar 2021, 1:30 PM
Shadow
Shadow - avatar
+ 2
Thanks. I know it's a beginner exercise, but as my solution was very ambiguous, I wanted a cleaner one ;)
16th Mar 2021, 1:43 PM
Riccardo Barbarossa
Riccardo Barbarossa - avatar