- 3

Take a number N as input and output the sum of all numbers from 1 to N (including N). Python code

No one likes homework, but your math teacher has given you an assignment to find the sum of the first N numbers. Let’s save some time by creating a program to do the calculation for you! Take a number N as input and output the sum of all numbers from 1 to N (including N).

17th Mar 2021, 5:32 PM
Pratik Chordiya
Pratik Chordiya - avatar
30 Answers
+ 38
n=int(input()) print((n+1)*n//2) This is perfectly right code
18th Mar 2021, 11:26 AM
Pratik Chordiya
Pratik Chordiya - avatar
+ 24
N = int(input()) x=0 for i in range (0, N+1): x+=i print(x) this worked for me perfectly
1st Feb 2022, 10:14 PM
Salome Mkheidze
Salome Mkheidze - avatar
+ 6
Pratik Chordiya 1) don't mark your own answer as best 2) this code is perfectly fine by itself, but the task expect you to practice loop ^^
18th Mar 2021, 11:28 AM
visph
visph - avatar
+ 6
N = int(input()) SUM = int() O = int() for O in range(0,N+1,1): SUM = SUM + O print(SUM)
21st Apr 2021, 7:50 PM
Sumaita Tasnime
+ 5
There is a formula to calculate sum of first n natural numbers which is, S = n(n+1)/2 You can solve this problem by applying the formula, n=int(input("Enter n: ")) print("Sum is:",n*(n+1)//2) But if you don't know the formula, no need to worry. You can simply use a looping construct to solve this problem. n,s=int(input("Enter limit: "), 0 for i in range(1,n+1): s+=i print("Sum:",s)
19th Mar 2021, 12:42 PM
ᗩηιηɗуα ᗩɗнιкαяι
ᗩηιηɗуα ᗩɗнιкαяι - avatar
+ 4
num = 358 if num < 0: print("Enter a positive number") else: sum = 0 while(num > 0): sum += num num -= 1 print(sum)
18th Mar 2021, 11:15 AM
Pratik Chordiya
Pratik Chordiya - avatar
+ 4
N = int(input()) SUM = int() O = int() for O in range(0,N+1,1): SUM = SUM + O print(SUM)
30th May 2021, 6:33 PM
alirezaie
alirezaie - avatar
+ 3
n = input("Enter N \n") n=int(n); total=0 for i in range(n+1): total+=i print("sum = "+str(total))
17th Mar 2021, 5:45 PM
deleted
+ 3
print((n+1)*n/2) I have the feeling it is YOUR time that is to be saved.
17th Mar 2021, 7:02 PM
Oma Falk
Oma Falk - avatar
+ 3
Thank u visph
18th Mar 2021, 11:24 AM
Pratik Chordiya
Pratik Chordiya - avatar
+ 3
N = int(input()) sum = 0 for x in range(N + 1): sum = sum + x print(sum) ##hope it helps
7th Feb 2022, 11:17 AM
Sandipan Karmakar
+ 2
(n+1)*n//2
18th Mar 2021, 11:22 AM
visph
visph - avatar
+ 1
Your's both of code it's perfectly right thank u both of you. visph Frogged
18th Mar 2021, 11:26 AM
Pratik Chordiya
Pratik Chordiya - avatar
+ 1
(n+1)*n//2
8th Feb 2022, 5:34 PM
Abdelrahman Arfat mohamed
Abdelrahman Arfat mohamed - avatar
+ 1
n=int(input()) print((n+1)*n//2) done...!
5th Mar 2022, 3:49 PM
Nikhila
+ 1
N = int(input()) #your code goes here R = range(0 , N) print (sum(R)+N) Clean
17th Sep 2022, 1:27 PM
Godwin Emerald
Godwin Emerald - avatar
+ 1
No one likes homework, but your math teacher has given you an assignment to find the sum of the first N numbers. Let’s save some time by creating a program to do the calculation for you! Take a number N as input and output the sum of all numbers from 1 to N (including N). Sample Input 100 Sample Output 5050 Explanation: The sum of all numbers from 1 to 100 is equal to 5050. N = int(input()) SUM = int() O = int() for O in range(0,N+1,1): SUM = SUM + O print(SUM) for all doubt solving contact on this by telegram https://youtu.be/tOWQqOWieE4
17th Nov 2022, 10:41 AM
Janveer Singh
Janveer Singh - avatar
0
provide your code attempt to get helped...
17th Mar 2021, 5:36 PM
visph
visph - avatar
0
what did u tried so far? searching a ready made solution will leads you to nothing, even with a degree, your employer will ask you to run few assessment in order to confirm your coding abilities, so try to brainstorm now to receive the reward later.
17th Mar 2021, 5:45 PM
iTech
iTech - avatar
0
n=int(input()) print((n+1)*n/2) This code is right but this code output in float
18th Mar 2021, 11:21 AM
Pratik Chordiya
Pratik Chordiya - avatar