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?

28th Apr 2021, 6:10 PM
Mwsdo
+ 2
print(sum(range(0, N+1)))
21st May 2021, 5:29 PM
Neha
+ 1
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.
28th Apr 2021, 6:22 PM
M Umer
+ 1
Try this 👇👇👇 N = int(input()) print(N*(N+1)//2)
29th Apr 2021, 1:30 PM
Jackson Kairani Maina
+ 1
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
5th Nov 2021, 9:33 PM
Rahinatu Ako Husnah
0
Or you can try using the sum() function. N = int(input()) N+=1 num = list(range(N)) total = sum(num) print(total)
10th May 2021, 12:26 PM