How to make a code for sum of consecutive numbers | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

How to make a code for sum of consecutive numbers

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.

23rd Mar 2021, 2:59 PM
Jay Bhamare
Jay Bhamare - avatar
19 Answers
+ 38
N = int(input()) count = N x = range(1, N +1) for i in x: N = i + N print(N - count)
18th May 2021, 8:09 AM
Ислам Алб
Ислам Алб - avatar
+ 24
Sum of Consecutive numbers N=int(input ()) sum=0 for i in range(1, N+1) : sum=sum+i print(sum)
2nd Nov 2021, 3:04 AM
GAMASU. SRI DEEPTHI SWARUPA
GAMASU. SRI DEEPTHI SWARUPA - avatar
+ 11
Hints: ** List has a built in sum() function . ** range(int(input())+1) will return a range from 0 to input. ** Now use the sum() function . ** remember sum() is a function of list object not of range so you need to typecast range to list.
23rd Mar 2021, 3:06 PM
TOLUENE
TOLUENE - avatar
+ 6
here is a list comprehension that would work and below that is a pure math option i saw from a similar post. N = int(input()) total = sum([sub for sub in range(1, N+1)]) print(total) here is a pure math method. N = int(input()) total = (N*(N+1)//2) print(total)
23rd Mar 2021, 3:50 PM
you are smart. you are brave.
you are smart. you are brave. - avatar
+ 4
print(sum(range(int(input()) + 1)))
26th Mar 2022, 8:29 PM
Per Bratthammar
Per Bratthammar - avatar
+ 2
Simple solution without loop: a = N+1 print(sum(range(a)))
26th Oct 2022, 10:14 AM
Rafał Wroński
Rafał Wroński - avatar
+ 2
I used this and it passed: N = int(input()) nums = (N*(N+1)//2) print(nums)
11th Dec 2022, 10:33 PM
Rob Bob
Rob Bob - avatar
+ 1
N = int(input()) #your code goes here i = 0 sum = sum(range(i, N)) print (sum + N)
16th Sep 2022, 4:36 AM
Vu Ba Luc
Vu Ba Luc - avatar
+ 1
n = int(input()) x = 0 for i in range(1,n+1): y = x+i x+=i print(y)
18th Sep 2022, 6:18 PM
Nathan
+ 1
N = int(input ()) s = 0 for i in range(s, N+1) : s += i print(s)
30th Sep 2022, 8:05 AM
Carlos Santiago Uceta Abad
23rd Mar 2021, 5:35 PM
Per Bratthammar
Per Bratthammar - avatar
0
N = int(input()) a = list(range(0,N+1)) Total = sum(a) print (Total)
26th Mar 2022, 8:00 PM
Nick Thomas
Nick Thomas - avatar
0
N=int(input("Enter your number:")) x=0 for i in range(1,N+1)
20th Jul 2022, 7:15 PM
Allen Kizito
Allen Kizito - avatar
0
N = int(input()) yeet = N zep = range(1, N+1) sum = 0 for num in zep: sum += num print(sum)
18th Aug 2022, 8:52 AM
Leonero
Leonero - avatar
0
N = int(input()) list = [] for i in range(1, N + 1): list.append(i) print(sum(list))
5th Nov 2022, 10:36 PM
Igor Mariano
Igor Mariano - avatar
0
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.
14th Jan 2023, 7:26 AM
GORLE SAI SREE VARSHINI
GORLE SAI SREE VARSHINI - avatar
0
N = int(input()) sum= int(n*(n+1)/2) print(sum)
20th Feb 2023, 8:44 PM
Alican Can
Alican Can - avatar
0
print(sum(range(1, N+1))) I was overthinking it but ended up with this one-liner and it worked lol
1st Apr 2023, 5:50 PM
Rach W
Rach W - avatar
0
N = int(input()) total = 0 for i in range(0, N): total += i print(total + N)
14th Apr 2023, 9:32 PM
Paweł Mikołaj Goleń
Paweł Mikołaj Goleń - avatar