0% found this document useful (0 votes)
5 views

Python Case Study Questions with Answers

The document contains a series of Python case study questions and their corresponding answers, covering various programming concepts. Topics include calculating factorials, Fibonacci sequences, GCD, prime checking, sorting algorithms (bubble sort, insertion sort, merge sort), binary search, sum of digits, and generating permutations. Each question is followed by a Python code implementation demonstrating the solution.

Uploaded by

soorajshangu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Python Case Study Questions with Answers

The document contains a series of Python case study questions and their corresponding answers, covering various programming concepts. Topics include calculating factorials, Fibonacci sequences, GCD, prime checking, sorting algorithms (bubble sort, insertion sort, merge sort), binary search, sum of digits, and generating permutations. Each question is followed by a Python code implementation demonstrating the solution.

Uploaded by

soorajshangu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Python Case Study Questions with Answers:

Question: Develop a Python program to find the factorial of a non-negative


integer using iteration.
Answer:
def factorial(n):
result = 1
for i in range(1, n + 1):
result *= i
return result

num = int(input("Enter a non-negative integer: "))


print("Factorial of", num, "is", factorial(num))

Question: Write a Python program to calculate the Fibonacci sequence up to a


specified limit using recursion.

Answer:
def fibonacci(n):
if n <= 1:
return n
else:
return fibonacci(n - 1) + fibonacci(n - 2)

limit = int(input("Enter the limit for Fibonacci sequence: "))


print("Fibonacci sequence:")
for i in range(limit):
print(fibonacci(i), end=" ")
Question: Develop a Python program to find the greatest common divisor (GCD)
of two numbers using the Euclidean algorithm.

Answer:
def gcd(a, b):
while b:
a, b = b, a % b
return a

num1 = int(input("Enter the first number: "))


num2 = int(input("Enter the second number: "))
print("GCD of", num1, "and", num2, "is", gcd(num1, num2))

Question: Write a Python program to check if a given number is prime using trial
division.
Answer:
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return False
return True

num = int(input("Enter a number: "))


if is_prime(num):
print(num, "is prime.")
else:
print(num, "is not prime.")
Question: Develop a Python program to sort a list of integers using the bubble
sort algorithm.
Answer:
def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]

numbers = [int(x) for x in input("Enter space-separated numbers: ").split()]


bubble_sort(numbers)
print("Sorted numbers:", numbers)

Question: Write a Python program to implement the binary search algorithm to


find the index of a given element in a sorted list.
Answer:
def binary_search(arr, target):
low, high = 0, len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1

numbers = [int(x) for x in input("Enter sorted space-separated numbers: ").split()]


target = int(input("Enter the target number: "))
index = binary_search(numbers, target)
if index != -1:
print("Target found at index", index)
else:
print("Target not found.")

Question: Develop a Python program to implement the insertion sort algorithm


to sort a list of integers.
Answer:
def insertion_sort(arr):
for i in range(1, len(arr)):
key = arr[i]
j=i-1
while j >= 0 and arr[j] > key:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key

numbers = [int(x) for x in input("Enter space-separated numbers: ").split()]


insertion_sort(numbers)
print("Sorted numbers:", numbers)

Question: Write a Python program to find the sum of digits of a given integer
using recursion.
Answer:
def sum_of_digits(n):
if n == 0:
return 0
else:
return n % 10 + sum_of_digits(n // 10)
num = int(input("Enter an integer: "))
print("Sum of digits:", sum_of_digits(num))

Question: Develop a Python program to generate all permutations of a given list


of elements using recursion.
Answer:
def permutations(lst):
if len(lst) == 0:
return [[]]
result = []
for i in range(len(lst)):
rest = lst[:i] + lst[i + 1:]
for perm in permutations(rest):
result.append([lst[i]] + perm)
return result

elements = input("Enter space-separated elements: ").split()


perms = permutations(elements)
print("Permutations:", perms)

Question: Write a Python program to implement the merge sort algorithm to sort
a list of integers.
Answer:
ef merge_sort(arr):
if len(arr) > 1:
mid = len(arr) // 2
left_half = arr[:mid]
right_half = arr[mid:]
merge_sort(left_half)
merge_sort(right_half)
i=j=k=0
while i < len(left_half) and j < len(right_half):
if left_half[i] < right_half[j]:
arr[k] = left_half[i]
i += 1
else:
arr[k] = right_half[j]
j += 1
k += 1
while i < len(left_half):
arr[k] = left_half[i]
i += 1
k += 1
while j < len(right_half):
arr[k] = right_half[j]
j += 1
k += 1

numbers = [int(x) for x in input("Enter space-separated numbers: ").split()]


merge_sort(numbers)
print("Sorted numbers:", numbers)

You might also like