Title: Sum of n natural numbers
Natural numbers are
1, 2,3,4,…………infinity
Sum of 5 natural numbers are
1+2+3+4+5=15
Program-1:
n=int(input("Enter n value\n"))
sum=0
for i in range(1,n+1):
sum=sum+i
print("The sum of ",n," natural numbers is ",sum)
Output:
Enter n value
5
The sum of 5 natural numbers is 15
Program-2:
n=int(input("Enter n value\n"))
sum=0
for i in range(n,0,-1):
sum=sum+i
print("The sum of ",n," natural numbers is ",sum)
Output:
Enter n value
5
The sum of 5 natural numbers is 15
Comments
Post a Comment