Monday, 2 March 2020

Python Programs

#Program to print natural numbers
n=int(input("enter number"))
for i in range(1,n+1):
  print(i,end=' ')

#Program to print 1 to n natural numbers and find the sum also
n=int(input("enter n value"))
print("natural numbers between 1 to {0}".format(n))
sum=0
for i in range(1,n+1):
   print(i,end=' ')
   sum=sum+i
print("\nsum of natural numbers are:{0}".format(sum))

#Reading and Printing of array elements
a=[]
n=int(input("enter n value"))
for i in range(n):
  a.append(int(input("enter elements into an array")))
for i in range(n):
   print(a[i]) 

# Calculate Factorial of a given number using functions
def fact(n):
   f=1;
   for i in range(1,n+1):
     f=f*i
   return f
n=int(input("enter n value"))
print("factorial of {0} is {1}".format(n,fact(n)))