+ 1

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
47 Antworten
+ 61
n=int(input()) print((n+1)*n//2) This is perfectly right code
18th Mar 2021, 11:26 AM
Pratik Chordiya
Pratik Chordiya - avatar
+ 48
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
+ 14
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
+ 13
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
+ 7
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
ᗩηιηɗуα ᗩɗнιкαяι
+ 4
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
+ 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
+ 4
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
+ 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
+ 2
(n+1)*n//2
18th Mar 2021, 11:22 AM
visph
visph - avatar
+ 2
#include <iostream> using namespace std; int main() { int n; cin >> n; int sum=0; for(int i=1;i<=n;i++) sum+=i; cout << sum; return 0; }
30th Aug 2023, 11:04 AM
Maryam Zamanian
+ 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
RAJVEER SINGH BAIRWA
RAJVEER SINGH BAIRWA - avatar
+ 1
N = int(input()) num = int() sum=0 for num in range(0,N+1): sum += num print(sum) This code works. Try it
8th Dec 2022, 9:29 AM
Gambo Victory
Gambo Victory - avatar
+ 1
N = int(input()) #your code goes here print(sum(range(1,N+1))) Very easy to solve this problem
10th Dec 2022, 8:22 PM
Long Ngô K16_HL
Long Ngô K16_HL - avatar