+ 3
Take a number N as input and output the sum of all numbers feom 1 to N (including N). Write a program to calculate
I'm so confused and lost
9 ответов
+ 5
Did you make an attempt? What's your code so far?
+ 5
Almost correct. But, instead of incrementing "sum" by 1, you need to increment it by each list item, which is "i", as it loops through the list.
You can see my code here:
https://code.sololearn.com/cgy2LrQPvse2/?ref=app
+ 4
Several things wrong ,
1: variable name is "sum" which is a built in method change it to something like "number_count" etc..
2: inside your for loop you're just adding 1 , youre not adding each number so that 1 in your loop should be i
3: again youre using a built in method , just print the variable
+ 4
#I know two way to do this ->
# first - doing simply
num = int(input())
sum = 0
for i in range(num+1):
sum += i
print(sum)
#taking number
# create a variable for storing sum of num
# for loop with range num+1, so num also include for sum
# adding values to sum
#print sum
#second - mathematical formula
num = int(input())
sum = num*(num+1)/2
print(sum)
#taking number
# using mathematical formula
# print sum
+ 2
Ace
total = N * (N + 1) // 2
+ 2
N=int(input("Enter value of N:"))
i=1
sum=0
x=list(range(0,N+1))
for i in x :
sum+=i
print (sum)
This is the solution. Please run the code . You will get the expected output.
0
Here is my code;
N = int(input ())
sum = 0
for i in range(1, N+1):
sum += 1
print(sum(N))
0
Thanks Ion 👍🏾
0
Why use this ?