0% found this document useful (0 votes)
11 views9 pages

Python-Mid-Term Exam Practice

The document contains a series of programming problems designed for a mid-term exam in a computer science course. Each problem requires the use of recursion to solve tasks such as counting digits, summing digits, reversing numbers, checking for palindromes, calculating powers, finding the GCD, generating Fibonacci series, and computing factorials. Example code snippets and expected outputs are provided for each problem.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views9 pages

Python-Mid-Term Exam Practice

The document contains a series of programming problems designed for a mid-term exam in a computer science course. Each problem requires the use of recursion to solve tasks such as counting digits, summing digits, reversing numbers, checking for palindromes, calculating powers, finding the GCD, generating Fibonacci series, and computing factorials. Example code snippets and expected outputs are provided for each problem.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Professor Dr.

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

def reverse_number(n, result=0):


if n == 0:
return result
else:
return reverse_number(n // 10, result * 10 + n
% 10)

# Example use
num = int(input("Enter a number: "))
print(f"Reversed number is
{reverse_number(num)}")
Practice for Mid-term Exam and Good luck!

Problem 4. Palindrome Checker (Recursion)


Problem: Ask the user for a word, and write a
recursive function to check if the word is a def is_palindrome(s):
palindrome. if len(s) <= 1:
Example: return True
elif s[0] != s[-1]:
Output: return False
Input: radar else:
Output: It is a palindrome! return is_palindrome(s[1:-
1])

# 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

def power(a, b):


if b == 0:
return 1
else:
return a * power(a, b - 1)

# 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!

Problem 6. GCD (Greatest Common Divisor)


Problem: Write a recursive function to find the GCD of two numbers using
Euclid's algorithm.

Output:
Input: 48, 18
Output: GCD is 6

def gcd(a, b):


if b == 0:
return a
else:
return gcd(b, a % b)

# 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)

Output: # Main program


Input: 5 num = int(input("Enter a number:
Output: "))
1! = 1
2! = 2 print("Factorial series:")
3! = 6 for i in range(1, num + 1):
print(f"{i}! = {factorial(i)}")
4! = 24

You might also like