Take a number N as input and output the sum of all numbers feom 1 to N (including N). Write a program to calculate | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

1st Jan 2023, 6:19 PM
CodeName47
CodeName47 - avatar
9 Answers
+ 5
Did you make an attempt? What's your code so far?
1st Jan 2023, 6:26 PM
Lamron
Lamron - avatar
+ 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
1st Jan 2023, 6:39 PM
Lamron
Lamron - avatar
+ 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
1st Jan 2023, 7:06 PM
Ion Kare
Ion Kare - avatar
+ 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
2nd Jan 2023, 3:21 PM
Parveen
Parveen - avatar
+ 2
Ace total = N * (N + 1) // 2
2nd Jan 2023, 8:45 AM
A͢J
A͢J - avatar
+ 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.
3rd Jan 2023, 9:21 AM
Dipanita Saha
Dipanita Saha - avatar
0
Here is my code; N = int(input ()) sum = 0 for i in range(1, N+1): sum += 1 print(sum(N))
1st Jan 2023, 6:37 PM
CodeName47
CodeName47 - avatar
0
Thanks Ion 👍🏾
1st Jan 2023, 7:14 PM
CodeName47
CodeName47 - avatar
0
Why use this ?
3rd Jan 2023, 12:03 PM
Erdal Kaymak
Erdal Kaymak - avatar