Programming With Python Practical 5

Question 1
Print the following patterns using loop.
pattern1.py
for i in range(1,5):
    for j in range(1,i+1):
        print("*",end="\t")
    print()
Output
pattern2.py
for i in(1,2,3,2,1):
    for k in range(3-i,0,-1):
        print(end="\t")
    for j in range(1,2*i):
        print("*",end="\t")
    print()
Output
pattern3.py
for i in range(1,5):
    for k in range(1,i):
        print(end="\t")
    for j in range(8,2*i-1,-1):
        if(j%2==0):
            print("1",end="\t")
        else:
            print("0",end="\t")
    print()
Output
Question 2
Write a Python program to print all even numbers between 1 to 100 using while loop.
evennumbers.py
print("Even numbers between 1 to 100")
i=2
while(i<=100):
    print("\t",i,end="\t")
    i=i+2;
Output
Question 3
Write a Python program to find the sum of first 10 natural numbers using for loop.
sum.py
sum=0
for i in range(1,11):
    sum=sum+i 
print("Sum of first 10 natural numbers= ",sum)
   
Output
Question 4
Write a Python program to print Fibonacci series.
fibonacci.py
first=0
second=1
third=0
num=int(input("Enter number of terms in fibonacci series: "))
print(first,end="\t")
print(second,end="\t")
for i in range(1,num-1):
    third=first+second
    print(third,end="\t")
    first=second
    second=third
   
Output
Question 5
Write a Python program to calculate factorial of a number.
factorial.py
num=int(input("Enter a number: "))
fact=1
while(num>=1):
	fact=fact*num
	num=num-1
print("Factorial of number is: ",fact)
Output
Question 6
Write a Python Program to Reverse a Given Number.
reverse.py
num=int(input("Enter a number: "))
n=num 
rem=rev=0
while(num>0):
	rem=int(num%10)
	rev=int(rev*10+rem)
	num=int(num/10)
print("Reverse of a number is: ",rev)
Output
Question 7
Write a Python program takes in a number and finds the sum of digits in a number.
sumofdigits.py
num=int(input("Enter a number: "))
n=num 
rem=sum=0
while(num>0):
	rem=int(num%10)
	sum=sum+rem
	num=int(num/10)
print("Sum of digits of a number is: ",sum)
Output
Question 8
Write a Python program that takes a number and checks whether it is a palindrome or not.
palindrom.py
num=int(input("Enter a number: "))
n=num 
rem=rev=0
while(num>0):
	rem=int(num%10)
	rev=int(rev*10+rem)
	num=int(num/10)
if(n==rev):
    print("number is a palindrome")
else:
    print("number is not a palindrome")
Output

Leave a Comment

Your email address will not be published. Required fields are marked *