Q.8 Display the terms of a Fibonacci series.
def fibonacci(n):
if n <= 0:
return []
elif n == 1:
return [0]
elif n == 2:
return [0, 1]
else:
fib_list = [0, 1]
while len(fib_list) < n:
next_term = fib_list[-1] + fib_list[-2]
fib_list.append(next_term)
return fib_list
n = int(input("Enter the number of terms to display: "))
print("The terms of the Fibonacci series:", fibonacci(n))
Output
Enter the number of terms to display: 10
The terms of the Fibonacci series: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
Q.9 Compute the greatest common divisor and least common
multiple of two integers.
def gcd(a, b):
while b != 0:
a, b = b, a % b
return a
def lcm(a, b):
return a * b // gcd(a, b)
a = int(input("Enter the first integer: "))
b = int(input("Enter the second integer: "))
gcd_value = gcd(a, b)
lcm_value = lcm(a, b)
print("The GCD of", a, "and", b, "is", gcd_value)
print("The LCM of", a, "and", b, "is", lcm_value)
Output
Enter the first integer: 18
Enter the second integer: 61
The GCD of 18 and 61 is 1
The LCM of 18 and 61 is 1098
Q.10 Count and display the number of vowels, consonants,
uppercase, lowercase characters in string.
def count_characters(string):
vowels = "AEIOUaeiou"
consonants = "BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz"
uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
lowercase = "abcdefghijklmnopqrstuvwxyz"
vowel_count = 0
consonant_count = 0
uppercase_count = 0
lowercase_count = 0
4 | P a g e