0

Hi everyone please help me to create a program that calculates the sum of conservative numbers

The program takes N number as input and it must calculate the sum of all numbers from 1 to that number Sample input 100 Sample out put 5050 Here is my code please help me to fix it N = int(input()) N=list(range(1,N+1)) sum=0 for x in N : sum+=x print(sum)

23rd Aug 2022, 7:58 AM
Farai Tambo
7 Respuestas
+ 3
You print 'sum' every time the variable increases. Think about that how to print it only once.
23rd Aug 2022, 8:15 AM
gonel
gonel - avatar
+ 1
Farai Tambo If you convert range (1, N + 1) to list then no need to use loop Just use sum function: N = int(input()) N = list(range(1, N + 1)) print (sum(N))
23rd Aug 2022, 9:26 AM
A͢J
A͢J - avatar
+ 1
A͢J there is no need to convert range to list. You can use the sum() function on a range directly.
4th Oct 2022, 9:29 PM
Евгений
Евгений - avatar
0
Look carefully where are you doing mistake. N = int(input()) sum = 0 for x in range(1,N+1): sum += x print(sum)
23rd Aug 2022, 8:18 AM
Darpan kesharwani
Darpan kesharwani - avatar
0
By human talent, you can output it as (2a+(n-1)d)n/2😂😂 PS. it is dangerous to use same variable name for different objects. There is a sum(list) function.
23rd Aug 2022, 8:23 AM
abpatrick catkilltsoi
abpatrick catkilltsoi - avatar
0
Declare a variable Sum assign it to 0 as it will be increased in the for loop as it iterates through until the before the final number N, that's why it's important to add N to the Sum so as to include the sum of all the numbers in that range and N. Try the code below, hope it's helpful. Sum = 0 N = int(input()) for i in range(0,N): Sum += i Sum += N print(Sum)
24th Aug 2022, 10:02 AM
Derrick Okello
0
Евгений I know but check his code.
5th Oct 2022, 3:18 AM
A͢J
A͢J - avatar