+ 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).
47 Réponses
+ 61
n=int(input())
print((n+1)*n//2)
This is perfectly right code
+ 48
N = int(input())
x=0
for i in range (0, N+1):
x+=i
print(x)
this worked for me perfectly
+ 14
N = int(input())
SUM = int()
O = int()
for O in range(0,N+1,1):
SUM = SUM + O
print(SUM)
+ 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 ^^
+ 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)
+ 4
n = input("Enter N \n")
n=int(n);
total=0
for i in range(n+1):
total+=i
print("sum = "+str(total))
+ 4
num = 358
if num < 0:
print("Enter a positive number")
else:
sum = 0
while(num > 0):
sum += num
num -= 1
print(sum)
+ 4
N = int(input())
SUM = int()
O = int()
for O in range(0,N+1,1):
SUM = SUM + O
print(SUM)
+ 4
N = int(input())
sum = 0
for x in range(N + 1):
sum = sum + x
print(sum)
##hope it helps
+ 3
print((n+1)*n/2)
I have the feeling it is YOUR time that is to be saved.
+ 3
Thank u visph
+ 2
(n+1)*n//2
+ 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;
}
+ 1
(n+1)*n//2
+ 1
n=int(input())
print((n+1)*n//2)
done...!
+ 1
N = int(input())
#your code goes here
R = range(0 , N)
print (sum(R)+N)
Clean
+ 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
+ 1
N = int(input())
num = int()
sum=0
for num in range(0,N+1):
sum += num
print(sum)
This code works. Try it
+ 1
N = int(input())
#your code goes here
print(sum(range(1,N+1)))
Very easy to solve this problem