Python
Python
Name—Lavanya Bawane
UID – 23003014
Branch – ETC
Q1. Write a python program to check whether a number is a palindrome.
num=3663
temp=num
reverse=0
while temp>0:
remainder=temp%10
reverse=(reverse*10)+remainder
temp=temp//10
if num==reverse:
print(“Palindrome”)
else:
print(“Not palindrome”)
O/P - Palindrome
Q2. Program to find the sum of digits of a given number.
n = 12345
sum = 0
while n > 0:
sum += n % 10
n //= 10
print(sum)
O/p – 15
Q3. Write a program to generate and print first n Fibonacci numbers using loop.
nterms = int(input("How many terms? "))
first two terms
n1, n2 = 0, 1
count = 0
if nterms <= 0:
print("Please enter a positive integer")
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)
nth = n1 + n2
n1 = n2
n2 = nth
count += 1
O/P - How many terms? 7
Fibonacci sequence:
0
1
1
2
3
5
8
Q4. Implement a program to check if a number is a strong number(sum of factorials of digits
equal the number).
import math
def is_strong_number(num):
if num < 0:
return False
digits = str(num)
sum_of_factorials = sum(math.factorial(int(digit)) for digit in digits)
return sum_of_factorials == num
if is_strong_number(number):
print(f"{number} is a Strong Number!")
else:
print(f"{number} is not a Strong Number.")
O/P - Please enter any number: 145
145 is a Strong Number!
Q6. Write a recursive function to compute the sum of first n natural number.
def sum_of_natural_numbers(n):
if n == 1:
return 1
else:
return n + sum_of_natural_numbers(n - 1)
n = 10
result = sum_of_natural_numbers(n)
print(f"The sum of the first {n} natural numbers is: {result}")
O/P - The sum of the first 10 natural numbers is: 55
def recur_factorial(n):
if n == 1:
return n
else:
return n*recur_factorial(n-1)
num = 7
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
print("The factorial of", num, "is", recur_factorial(num))
O\P - The factorial of 7 is 5040
Q8. Write a recursive function to count the number of digits in a given numbers.
def recur(n):
if n == 0:
return 0
return 1 + recur(n // 10)
number = 12345
print(f"The number of digits in {number} is: {recur(number)}")
O/P - The number of digits in 12345 is: 5
lst = [3, 2, 3, 4, 5]
print(f"The sum of elements is: {recur_sum(lst)}")
O/P - The sum of elements is: 17
Q10. Write a recursive function to generate the binary representation of a given decimal
number.
def decimal_to_binary(n):
if n == 0:
return ''
return decimal_to_binary(n // 2) + str(n % 2)
number = 10
binary_representation = decimal_to_binary(number)
print(f"The binary representation of {number} is: {binary_representation or '0'}")
O/P- The binary representation of 10 is: 1010
Q11. Write a program to find the second largest element in a given list.
a = [10, 20, 4, 45, 99]
max1 = max2 = float('-inf')
for n in a:
if n > max1:
max2 = max1
max1 = n
elif n > max2 and n != max1
max2 = n
print(max2)
0/P – 45
Q12. Implement a program to remove all duplicate from a list without using set() function.
def remove_duplicates(lst):
unique_lst = []
for item in lst:
if item not in unique_lst:
unique_lst.append(item)
return unique_lst
lst = [1, 2, 3, 2, 4, 5, 3, 6, 5]
print(f"List after removing duplicates: {remove_duplicates(lst)}")
O/P - List after removing duplicates: [1, 2, 3, 4, 5, 6]
Q13. Write a python program to rotate a list to the right by k position in python.
def rotate_list(lst, k):
k = k % len(lst)
return lst[-k:] + lst[:-k]
lst = [1, 2, 3, 4, 5, 6]
k=2
print(f"List after rotating {k} positions to the right: {rotate_list(lst, k)}")
O/P - List after rotating 2 positions to the right: [5, 6, 1, 2, 3, 4]
Q14. Implement a function to check if two strings are anagrams of each other.
def are_anagrams(str1, str2):
return Counter(str1) == Counter(str2)
# Example Usage
print(are_anagrams("listen", "silent")) # True
print(are_anagrams("hello", "world")) # False
# Example Usage
string = "hello world"
char_count = count_characters(string)
print(char_count)
O/P - {'h': 1, 'e': 1, 'l': 3, 'o': 2, ' ': 1, 'w': 1, 'r': 1, 'd': 1}
16. Write a Python function to check whether a string is a valid password (must contain at
least one uppercase letter, one lowercase letter, one number, and one special character).