+ 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.

19 odpowiedzi

+ 38

N = int(input())
count = N
x = range(1, N +1)
for i in x:
N = i + N
print(N - count)

+ 24

Sum of Consecutive numbers
N=int(input ())
sum=0
for i in range(1, N+1) :
sum=sum+i
print(sum)

+ 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.

+ 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)

+ 4

print(sum(range(int(input()) + 1)))

+ 2

Simple solution without loop:
a = N+1
print(sum(range(a)))

+ 2

I used this and it passed:
N = int(input())
nums = (N*(N+1)//2)
print(nums)

+ 1

N = int(input())
#your code goes here
i = 0
sum = sum(range(i, N))
print (sum + N)

+ 1

n = int(input())
x = 0
for i in range(1,n+1):
y = x+i
x+=i
print(y)

+ 1

N = int(input ())
s = 0
for i in range(s, N+1) :
s += i
print(s)

0

Hi! Here are some examples:
https://code.sololearn.com/cjrtDK1l5uiC/?ref=app

0

N = int(input())
a = list(range(0,N+1))
Total = sum(a)
print (Total)

0

N=int(input("Enter your number:"))
x=0
for i in range(1,N+1)

0

N = int(input())
yeet = N
zep = range(1, N+1)
sum = 0
for num in zep:
sum += num
print(sum)

0

N = int(input())
list = []
for i in range(1, N + 1):
list.append(i)
print(sum(list))

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.

0

N = int(input())
sum= int(n*(n+1)/2)
print(sum)

0

print(sum(range(1, N+1)))
I was overthinking it but ended up with this one-liner and it worked lol

0

N = int(input())
total = 0
for i in range(0, N):
total += i
print(total + N)