SOLVED PYTHON PROGRAMS FOR REVISION
Q1. Menu Driven Program – Largest / Buzz Number
Question:
Write a menu driven program using match-case to perform the following
operations:
1. Input three numbers and print the largest number.
2. Input a number and check whether it is a Buzz number or not. A number which is
divisible by 7 or whose last digit is 7 is called a Buzz number.
Include a default case for invalid choice.
print("1. Largest of three numbers")
print("2. Buzz number check")
c = int(input("Enter choice: "))
match c:
case 1:
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
d = int(input("Enter third number: "))
if a > b and a > d:
print("Largest =", a)
elif b > a and b > d:
print("Largest =", b)
else:
print("Largest =", d)
case 2:
n = int(input("Enter number: "))
if n % 7 == 0 or n % 10 == 7:
print("Buzz Number")
else:
print("Not a Buzz Number")
case _:
print("Invalid Choice")
Q2. Menu Driven Program – Area Calculations
Question:
Write a menu driven program using match-case to perform the
following: 1. Input length and breadth of a rectangle and print its area.
2. Input radius of a circle and print its area.
Include a default case.
print("1. Area of Rectangle")
print("2. Area of Circle")
c = int(input("Enter choice: "))
match c:
case 1:
l = int(input("Enter length: "))
b = int(input("Enter breadth: "))
a=l*b
print("Area =", a)
case 2:
r = int(input("Enter radius: "))
a = 3.14 * r * r
print("Area =", a)
case _:
print("Invalid Choice")
Q3. Menu Driven Program – Leap Year / Voting Eligibility
Question:
Write a menu driven program using match-case to perform the
following: 1. Input a year and check whether it is a leap year or not.
2. Input the age of a person and check whether the person is eligible to vote or not.
Include a default case.
print("1. Leap Year Check")
print("2. Voting Eligibility")
c = int(input("Enter choice: "))
match c:
case 1:
y = int(input("Enter year: "))
if y % 400 == 0 or (y % 4 == 0 and y % 100 != 0):
print("Leap Year")
else:
print("Not a Leap Year")
case 2:
a = int(input("Enter age: "))
if a >= 18:
print("Eligible to vote")
else:
print("Not eligible to vote")
case _:
print("Invalid Choice")
Q4. Menu Driven Program – Triangle / Sum & Average
Question:
Write a menu driven program using match-case to perform the following:
1. Input three sides of a triangle and check whether the triangle is possible or not.
(Sum of any two sides must be greater than the third side.)
2. Input three numbers and print their sum and average.
Include a default case.
print("1. Triangle Validity")
print("2. Sum and Average")
c = int(input("Enter choice: "))
match c:
case 1:
a = int(input("Enter side 1: "))
b = int(input("Enter side 2: "))
d = int(input("Enter side 3: "))
if a + b > d and a + d > b and b + d > a:
print("Triangle is possible")
else:
print("Triangle is not possible")
case 2:
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
d = int(input("Enter third number: "))
s=a+b+d
v=s/3
print("Sum =", s)
print("Average =", v)
case _:
print("Invalid Choice")
Q5. Perfect Number
Question:
Write a program to input a number and check whether it is a Perfect number or
not. A Perfect number is equal to the sum of its factors excluding the number itself.
n = int(input("Enter number: "))
s=0
for i in range(1, n):
if n % i == 0:
s=s+i
if s == n:
print("Perfect Number")
else:
print("Not a Perfect Number")
Q6. Prime Number
Question:
Write a program to input a number and check whether it is a Prime number or
not. A prime number has exactly two factors.
n = int(input("Enter number: "))
c=0
for i in range(1, n + 1):
if n % i == 0:
c=c+1
if c == 2:
print("Prime Number")
else:
print("Not a Prime Number")
Q7. Spy Number
Question:
Write a program to input a number and check whether it is a Spy number or
not. A Spy number is a number whose sum of digits is equal to the product of
digits.
n = int(input("Enter number: "))
s=0
p=1
while n > 0:
d = n % 10
s=s+d
p=p*d
n = n // 10
if s == p:
print("Spy Number")
else:
print("Not a Spy Number")
Q8. Palindrome Number
Question:
Write a program to input a number and check whether it is a Palindrome number or
not. A Palindrome number is a number which is equal to its reverse.
n = int(input("Enter number: "))
t=n
r=0
while n > 0:
d = n % 10
r = r * 10 + d
n = n // 10
if r == t:
print("Palindrome Number")
else:
print("Not a Palindrome Number")
Q9. Factorial
Question:
Write a program to input a number and print its factorial.
n = int(input("Enter number: "))
f=1
for i in range(1, n + 1):
f=f*i
print("Factorial =", f)
Q10. Function – Sum of Square Series
Question:
Design a function display() which accepts n as parameter and prints the sum of the
series: S = 1 + 4 + 9 + 16 + … n terms.
Call the function two times.
def display(n):
s=0
for i in range(1, n + 1):
s=s+i*i
print("Sum =", s)
display(5)
display(10)
Q11. Function – Special Series (Corrected)
Question:
Design a function display() which accepts n as parameter and prints the sum of the
series: S = 0 + 7 + 26 + 63 + … n terms.
(The general term of the series is i³ − 1.)
Call the function two times.
def display(n):
s=0
for i in range(1, n + 1):
t=i*i*i-1
s=s+t
print("Sum =", s)
display(5)
display(8)
Q12. Function – Fibonacci Series
Question:
Design a function display() which accepts n as parameter and prints Fibonacci series up to n
terms. Call the function two times.
def display(n):
a=0
b=1
for i in range(1,n+1):
c=a+b
print(a, end=" ")
a=b
b=c
display(7)
display(10)
Q13. Function – Fraction Series
Question:
Design a function display() which accepts n as parameter and prints the sum of the
series: S = 1/2 + 3/4 + 5/6 + … n terms.
Call the function two times.
def display(n):
s=0
a=1
b=2
for i in range(n):
s = s + (a / b)
a=a+2
b=b+2
print("Sum =", s)
display(5)
display(8)
Q14. Pattern
Programs (a)
12
123
1234
for i in range(1, 5): for j
in range(1, i + 1):
print(j, end="") print()
(b)
1234#
123#
12#
1#
for i in range(4, 0, -1):
for j in range(1, i + 1):
print(j, end="")
print("#")
(c)
11111
2222
333
44
n=5
for i in range(1, n + 1):
for j in range(n - i + 1):
print(i, end="")
print()
(d)
#4
#43
#432
#4321
for i in range(4, 0, -1):
print("#", end="") for j in
range(4, i - 1, -1): print(j,
end="")
print()
(e)
+++++
####
+++
##
for i in range(5, 0,
-1): for j in range(i):
if i % 2 != 0:
print("+", end="") else:
print("#", end="")
print()