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)
7 Respostas
+ 3
You print 'sum' every time the variable increases. Think about that how to print it only once.
+ 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))
+ 1
A͢J there is no need to convert range to list. You can use the sum() function on a range directly.
0
Look carefully where are you doing mistake.
N = int(input())
sum = 0
for x in range(1,N+1):
sum += x
print(sum)
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.
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)
0
Евгений
I know but check his code.