Python-Mid-Term Exam Practice
Python-Mid-Term Exam Practice
Iqbal Ahmed
Department of Computer Science and Engineerin
University of Chittagong
Practice for Mid-term Exam and Good luck!
Problem 1: Custom Pattern Counter using Recursion
Write a Python program using recursion to count how many times
the digit 7 appears in a number entered by the user.
Requirements:
•Use a recursive function to count how many times 7 occurs in the
number. def count_sevens(n):
•The input should be a positive integer. if n == 0:
•Do not use string conversion (like str() or slicing).
return 0
•Print the result with a clear message.
elif n % 10 == 7:
Output: return 1 + count_sevens(n // 10)
Enter a number: 7771237 else:
The digit 7 appears 4 times. return count_sevens(n // 10)
# User input
number = int(input("Enter a positive
integer: "))
if number < 0:
print("Please enter a positive
integer.")
else:
Practice for Mid-term Exam and Good luck!
Problem 2. Sum of Digits Using Recursion
Problem: Write a recursive function that returns the sum of all digits of a positive
integer entered by the user.
Output:
Input: 1234
Output: Sum of digits is 10
def sum_of_digits(n):
if n == 0:
return 0
else:
return (n % 10) + sum_of_digits(n //
10)
# Example use
num = int(input("Enter a number: "))
print(f"Sum of digits is
{sum_of_digits(num)}")
Practice for Mid-term Exam and Good luck!
Problem 3. Reverse a Number Recursively
Problem: Write a recursive function that reverses a number without using strings or
loops.
Output:
Input: 123
Output: Reversed number is 321
# Example use
num = int(input("Enter a number: "))
print(f"Reversed number is
{reverse_number(num)}")
Practice for Mid-term Exam and Good luck!
# Example use
word = input("Enter a word: ")
if is_palindrome(word):
print("It is a palindrome!")
else:
print("It is not a palindrome.")
Practice for Mid-term Exam and Good luck!
Problem 5. Power Calculator
Problem: Write a recursive function to calculate a^b (a raised to the power b),
where both a and b are input by the user.
Output:
Input: a = 2, b = 3
Output: 8
# Example use
a = int(input("Enter base (a): "))
b = int(input("Enter exponent (b): "))
print(f"{a} raised to the power {b} is
{power(a, b)}")
Practice for Mid-term Exam and Good luck!
Output:
Input: 48, 18
Output: GCD is 6
# Example use
x = int(input("Enter first number: "))
y = int(input("Enter second number:
"))
Practice for Mid-term Exam and Good luck!
Problem 7: Fibonacci Series Using Recursion
Task:
Write a Python program using recursion to print the Fibonacci series up to n terms,
where n is entered by the user.
Rules:
•The Fibonacci series starts as: 0, 1, 1, 2, 3, 5, 8, ...
def fibonacci(n):
•The first two terms are always 0 and 1.
if n == 0:
•Use a recursive function to calculate each term. return 0
elif n == 1:
Input: 5 return 1
Output: 0 1 1 2 3 else:
return fibonacci(n - 1) + fibonacci(n - 2)
# Main program
num = int(input("Enter how many terms of
Fibonacci series you want: "))
print("Fibonacci series:")
for i in range(num):
print(fibonacci(i), end=" ")
Practice for Mid-term Exam and Good luck!
Problem 8: Factorial Series Using
Recursion
Task:
Write a Python program using recursion to print
the factorial of each number from 1 to n,
where n is entered by the user.
Rules:
•Factorial of n is defined as n * (n-1) * (n-2) * ... * def factorial(n):
1. if n == 0 or n == 1:
return 1
•Use a recursive function to compute the
else:
factorial of each number. return n * factorial(n - 1)