1) FIBONACCI SERIES USING RECURSION
def fib(n):
if n <= 1:
return n
else:
return fib(n-1) + fib(n-2)
n = int(input("Enter the number: "))
for i in range(n):
print(fib(i))
2) Prime or Not Using Function
def prime(n):
if n <= 1:
print(n, "is not a prime number")
else:
temp = 0
for i in range(2, n):
if n % i == 0:
temp = 1
break
if temp == 1:
print(n, "is not a prime number")
else:
print(n, "is a prime number")
n = int(input("Enter a number: "))
prime(n)
3) OCCURRENCE OF VOWELS, CONS, QUESTION MARK IN A STRING
userInput = input("Enter a string: ")
userInput = userInput.lower()
vowel = 0
cons = 0
quest = 0
for characters in userInput:
if characters in "aeiou":
vowel += 1
elif characters in "?":
quest += 1
else:
cons += 1
print("Number of vowels:", vowel)
print("Number of consonants:", cons)
print("Number of question marks:", quest)
4) NCR USING RECURSION
def ncr(n, r):
if r == 0 or r == n:
return 1
else:
return ncr(n - 1, r - 1) + ncr(n - 1, r)
n = int(input("Enter the value of n: "))
r = int(input("Enter the value of r: "))
if n < r:
print("r cannot be greater than n in combinations.")
else:
print("nCr(", n, ",", r, ") =", ncr(n, r))
5) CONVERT LIST INTO DICTIONARY
keys = []
values = []
key = input("Enter the key: ")
value = input("Enter the value: ")
keys.append(key)
values.append(value)
print(keys, values)
result = dict(zip(keys, values))
print(result)
6) CREATE A DICTIONARY AND COUNT NO. OF VOWELS, CONS, NUMBERS, QUEST
userInput = input("Enter a string: ")
userInput = userInput.lower()
count = {"vowels": 0, "cons": 0, "quest": 0, "digits": 0}
for i in userInput:
if i in "aeiou":
count["vowels"] += 1
elif i == "?":
count["quest"] += 1
elif i.isdigit():
count["digits"] += 1
elif i.isalpha():
count["cons"] += 1
print(count)
7) ODD OR EVEN USING LAMBDA
a = int(input("Enter a number: "))
b = lambda a: a % 2
if b(a) == 0:
print(a, "is even")
else:
print(a, "is odd")
8) PERFECT NUMBER OR NOT USING RECURSION
def sumofdiv(n, i=1):
if i > n // 2:
return 0
elif n % i == 0:
return i + sumofdiv(n, i + 1)
else:
return sumofdiv(n, i + 1)
def is_perfect(n):
return n == sumofdiv(n)
n = int(input("Enter the number: "))
if is_perfect(n):
print(n, "is a perfect number")
else:
print(n, "is not a perfect number")
9) READ FIRST N LINES OF A TEXT FILE
with open("op.txt", 'w') as f:
f.write("Hello\nWorld\nWelcome\n")
n = int(input("Enter the number of lines to read: "))
with open("op.txt", 'r') as f:
for i in range(n):
line = f.readline()
if line:
print(line.strip())
else:
break
10) COUNT NO OF LINES IN A TEXT FILE
filename = "example.txt"
with open(filename, 'w') as f:
f.write("hello\n")
f.write("world\n")
f.write("my\n")
f.write("name\n")
with open(filename, 'r') as f:
lines = f.readlines()
count = len(lines)
print("Count of the lines =", count)
11) OCCURRENCE OF A SUBSTRING
a = input("Enter a string: ")
b = input("Enter the substring: ")
occurrence = a.count(b)
print("Occurrence times:", occurrence)
12) PALINDROME USING STRING
n = input("Enter a word: ")
rev = n[::-1]
print("Reverse =", rev)
if n == rev:
print("Given word is a palindrome")
else:
print("Given word is not a palindrome")