0

# Sum of consecutive numbers

I have to calculate the sum of consecutive numbers from 1 to N This is my attempt at the code: N= int(input()) for x in range (1 , N): print (x+N) for example if the input is 380 my outputs are: 381 382 383 .... etc anyone can help with the flaw of my code?

4/28/2021 6:10:16 PM

Mwsdo

+7

You don't need a for-loop for this task. You only need one formula for the sum of all integer numbers from 1 to N: s = N*(N+1)/2

+4

print(sum(range(0, N+1)))

+3

Try this πππ N = int(input()) print(N*(N+1)//2)

+2

For example: for x in range (1, 100): print (x+100) This code will just add the N number to the iterator (x). You can do something like: y=0 for x in range (1, 100): y+=x print (y) Explanation: this will keep adding the numbers to 'y' until N is reached. Then it'll print the final one.

+2

N = int(input()) #your code goes here sum = 0 numbers = range(1,N + 1) for x in numbers: sum += x print(sum) Try this code

0

Or you can try using the sum() function. N = int(input()) N+=1 num = list(range(N)) total = sum(num) print(total)

0

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

0

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

0

This code works x = 358 * (358+1) / 2 print(int(x)) But as soon as I answer the 2nd test, it counts the first one as incorrect so Iβm not able to move onto the third. Is this happening for anyone else? Am I doing something wrong?

-3

CONGRATS your are the 1000th person who asks this queston. You won a personal class: sololearn for lazy persons. look here: https://code.sololearn.com/W0os7xWeVOGx/?ref=app now... that was fun. but problems like this are predestinated for searchbar. Please try to become familiar with it.