'Loop to cumpute sum of numbers 1 through n in python.for example if i say n=3 , it should show (6)=(1+2+3)
i cant write this program.i think it is need to a function.
number1=int(input("Enter your number : ")
number2=number1-1
number3=1+(number2-1)
Solution 1:[1]
you can write the code like :
number1=int(input("Enter your number : ")
def show(n):
s=0
for i in range(1,n+1):
s=s+i
print(s)
show(number1)
Solution 2:[2]
num = input("Enter a number to calculate sum")
num = int(num)
sum = num * (num + 1) / 2
print("Sum of the first ", num , "natural numbers using formula is: ", sum )
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Hrithik Pal |
| Solution 2 |
