+ 1

Sum of Consecutive Numbers

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 Explanation: The sum of all numbers from 1 to 100 is equal to 5050. You can iterate over a range and calculate the sum of all numbers in the range. Remember, range (a, b) does not include b, thus you need to use b+1 to include b in the range.

10th Jan 2023, 10:25 PM
Taha Nekhli
Taha Nekhli - avatar
6 Respuestas
+ 7
Taha Nekhli , # basic version using a for loop number = int(input()) total = 0 for num in range(1, number +1): total += num print(total) # version using sum() built-in function number = int(input()) print(sum(range(1, number +1)))
11th Jan 2023, 8:22 PM
Lothar
Lothar - avatar
+ 5
Taha Nekhli what does this [x+1:] here Why not to add x in a temp variable like total total = total + x range is already a function so you can't use as a variable so do this: for x in range (a, b + 1):
11th Jan 2023, 6:54 AM
A͢J
A͢J - avatar
+ 3
Hello. Do you have any attempts? Try to use **for** loop and **range** function
10th Jan 2023, 10:35 PM
Lamron
Lamron - avatar
+ 2
N = int(input()) #your code goes here a = 1 b = N range = [a,b+1] for x in range : print([x+1:]) I tried this but it doesn’t work
10th Jan 2023, 11:10 PM
Taha Nekhli
Taha Nekhli - avatar
+ 2
n = int(input()) print(n*(n+1)/2) Learn mathematics if you want to do so solutions..
11th Jan 2023, 8:15 AM
Smith Welder
Smith Welder - avatar
- 1
Hi guys please a solution for this thaanx
10th Jan 2023, 10:30 PM
Taha Nekhli
Taha Nekhli - avatar