Home > other >  Loop to cumpute sum of numbers 1 through n in python.for example if i say n=3 , it should show (6)=(
Loop to cumpute sum of numbers 1 through n in python.for example if i say n=3 , it should show (6)=(

Time:01-29

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)

CodePudding user response:

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)
        

CodePudding user response:

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 )

  •  Tags:  
  • Related