- 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).

30 Answers

+ 38

n=int(input())
print((n+1)*n//2)
This is perfectly right code

+ 24

N = int(input())
x=0
for i in range (0, N+1):
x+=i
print(x)
this worked for me perfectly

+ 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 ^^

+ 6

N = int(input())
SUM = int()
O = int()
for O in range(0,N+1,1):
SUM = SUM + O
print(SUM)

+ 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)

+ 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)

+ 3

n = input("Enter N \n")
n=int(n);
total=0
for i in range(n+1):
total+=i
print("sum = "+str(total))

+ 3

print((n+1)*n/2)
I have the feeling it is YOUR time that is to be saved.

+ 3

Thank u visph

+ 3

N = int(input())
sum = 0
for x in range(N + 1):
sum = sum + x
print(sum)
##hope it helps

+ 2

(n+1)*n//2

+ 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

0

provide your code attempt to get helped...

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.

0

n=int(input())
print((n+1)*n/2)
This code is right but this code output in float

Hot today

Somebody help me please 🥺

0 Votes

Kotlin get if Var doesn't exist

-1 Votes

difference in while loop

2 Votes