1
Q1. Determine whether a number is a perfect number, an Armstrong number or
palindrome
# CODE OF PROGRAM
def is_perfect_number(num):
divisors_sum = sum(i for i in range(1, num) if num % i == 0)
return divisors_sum == num
def is_armstrong_number(num):
num_str = str(num)
num_digits = len(num_str)
armstrong_sum = sum(int(digit) ** num_digits for digit in num_str)
return armstrong_sum == num
def is_palindrome(num):
num_str = str(num)
return num_str == num_str[::-1]
# Example usage:
number_to_check = 28 # Replace with the desired number
if is_perfect_number(number_to_check):
print(f"{number_to_check} is a perfect number.")
if is_armstrong_number(number_to_check):
print(f"{number_to_check} is an Armstrong number.")
if is_palindrome(number_to_check):
print(f"{number_to_check} is a palindrome.")
# OUTPUT OF PROGRAM
28 is a perfect number.
[Program finished]
2
Q2.Input a number and check if the number is a prime or composite number.
# CODE OF PROGRAM
def is_prime(num):
if num < 2:
return False
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
return False
return True
# Example usage:
number_to_check = int(input("Enter a number: "))
if is_prime(number_to_check):
print(f"{number_to_check} is a prime number.")
else:
print(f"{number_to_check} is a composite number.")
# OUTPUT OF PROGRAM
● Enter a number: 5
5 is a prime number.
● Enter a number: 10
10 is a composite number.
[Program finished]
3
Q3.Display the terms for a Fibonacci series.
# CODE OF PROGRAM
def fibonacci_series(n):
fib_series = [0, 1]
while len(fib_series) < n:
fib_series.append(fib_series[-1] + fib_series[-2])
return fib_series
# Example usage:
num_terms = int(input("Enter the number of terms for Fibonacci series: "))
result = fibonacci_series(num_terms)
print(f"Fibonacci series up to {num_terms} terms: {result}")
# OUTPUT OF PROGRAM
Enter the number of terms for Fibonacci series: 34
Fibonacci series up to 34 terms: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377,
610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393,
196418, 317811, 514229, 832040, 1346269, 2178309, 3524578]
[Program finished]
4
Q4.Compute the greatest common divisor and least common multiple of two integers.
# CODE OF PROGRAM
def compute_gcd(x, y):
while y:
x, y = y, x % y
return abs(x)
def compute_lcm(x, y):
gcd = compute_gcd(x, y)
lcm = abs(x * y) // gcd
return lcm
# Example usage:
num1 = int(input("Enter the first integer: "))
num2 = int(input("Enter the second integer: "))
gcd_result = compute_gcd(num1, num2)
lcm_result = compute_lcm(num1, num2)
print(f"GCD of {num1} and {num2} is: {gcd_result}")
print(f"LCM of {num1} and {num2} is: {lcm_result}")
# OUTPUT OF PROGRAM
Enter the first integer: 7
Enter the second integer: 9
GCD of 7 and 9 is: 1
LCM of 7 and 9 is: 63
[Program finished]
5
Q5.Count and display the number of vowels, consonants, uppercase, lowercase
characters in string.
# CODE OF PROGRAM
def count_characters(string):
vowels = "aeiouAEIOU"
consonants = "bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ"
num_vowels = sum(1 for char in string if char in vowels)
num_consonants = sum(1 for char in string if char in consonants)
num_uppercase = sum(1 for char in string if char.isupper())
num_lowercase = sum(1 for char in string if char.islower())
return num_vowels, num_consonants, num_uppercase, num_lowercase
# Example usage:
input_string = input("Enter a string: ")
vowels, consonants, uppercase, lowercase = count_characters(input_string)
print(f"Number of vowels: {vowels}")
print(f"Number of consonants: {consonants}")
print(f"Number of uppercase characters: {uppercase}")
print(f"Number of lowercase characters: {lowercase}")
# OUTPUT OF PROGRAM
Enter a string: Ritesh
Number of vowels: 2
Number of consonants: 4
Number of uppercase characters: 1
Number of lowercase characters: 5
[Program finished]
6
Q6.Input a string and determine whether it is a palindrome or not; convert the case of
characters in a string.
#A CODE OF PROGRAM
def is_palindrome(string):
clean_string = ''.join(char.lower() for char in string if char.isalnum())
return clean_string == clean_string[::-1]
def convert_case(string):
return string.swapcase()
# Example usage:
input_string = input("Enter a string: ")
if is_palindrome(input_string):
print("The entered string is a palindrome.")
else:
print("The entered string is not a palindrome.")
converted_string = convert_case(input_string)
print(f"String after converting the case: {converted_string}")
# OUTPUT OF PROGRAM
Enter a string: Ritesh
The entered string is not a palindrome.
String after converting the case: rITESH
[Program finished]
7
Q7.Find the largest/smallest number in a list/tuple
# CODE OF PROGRAM
def find_largest_smallest(numbers):
if not numbers:
return None, None # Return None for both largest and smallest if the list is empty
largest = smallest = numbers[0]
for num in numbers:
if num > largest:
largest = num
elif num < smallest:
smallest = num
return largest, smallest
# Example usage with a list:
number_list = [23, 45, 12, 67, 89, 34, 56]
largest_num, smallest_num = find_largest_smallest(number_list)
print(f"Largest number: {largest_num}")
print(f"Smallest number: {smallest_num}")
# OUTPUT OF PROGRAM
Largest number: 89
Smallest number: 12
[Program finished]
8
Q8. Input a list of numbers and swap elements at the even location with the elements at
the odd location.
# CODE OF PROGRAM
def swap_even_odd_locations(numbers):
for i in range(0, len(numbers)-1, 2):
numbers[i], numbers[i+1] = numbers[i+1], numbers[i]
return numbers
# Example usage:
input_numbers = input("Enter a list of numbers separated by spaces: ")
number_list = [int(num) for num in input_numbers.split()]
if len(number_list) % 2 == 0:
swapped_list = swap_even_odd_locations(number_list)
print(f"List after swapping elements at even and odd locations: {swapped_list}")
else:
print("The list should have an even number of elements for swapping.")
# OUTPUT OF PROGRAM
Enter a list of numbers separated by spaces: 1234
The list should have an even number of elements for swapping.
Enter a list of numbers separated by spaces: 72910
The list should have an even number of elements for swapping.
[Program finished]
9
Q9. Input a list/tuple of elements, search for a given element in the list/tuple.
# CODE OF PROGRAM
def search_element(container, element):
return element in container
# Example usage with a list:
input_list = input("Enter a list of elements separated by spaces: ")
element_list = input_list.split()
search_element_input = input("Enter the element to search: ")
result = search_element(element_list, search_element_input)
if result:
print(f"{search_element_input} is present in the list.")
else:
print(f"{search_element_input} is not present in the list.")
# OUTPUT OF PROGRAM
Enter a list of elements separated by spaces: 123345
Enter the element to search: 234
234 is not present in the list.
[Program finished]
10
Q10. Create a dictionary with the roll number, name and marks of n students in a class
and display
# CODE OF PROGRAM
def create_student_dict(n):
student_dict = {}
for _ in range(n):
roll_number = input("Enter Roll Number: ")
name = input("Enter Name: ")
marks = float(input("Enter Marks: "))
student_dict[roll_number] = {'Name': name, 'Marks': marks}
return student_dict
def display_student_dict(student_dict):
print("Student Information:")
for roll_number, info in student_dict.items():
print(f"Roll Number: {roll_number}, Name: {info['Name']}, Marks: {info['Marks']}")
# Example usage:
num_students = int(input("Enter the number of students: "))
students = create_student_dict(num_students)
display_student_dict(students)
# OUTPUT OF PROGRAM
Enter the number of students: 1
Enter roll number for student 1: 1
Enter name for student 1: Ritesh
Enter marks for student 1: 100
Student Information:
Roll Number: 1, Name: Ritesh, Marks: 100.0
[Program finished]
11
Q11. Input three numbers and display the largest /smallest number.
# CODE OF PROGRAM
def find_largest_smallest(num1, num2, num3):
largest = max(num1, num2, num3)
smallest = min(num1, num2, num3)
return largest, smallest
# Example usage:
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))
largest_num, smallest_num = find_largest_smallest(num1, num2, num3)
print(f"Largest number: {largest_num}")
print(f"Smallest number: {smallest_num}")
# OUTPUT OF PROGRAM
Enter the first number: 23
Enter the second number: 45
Enter the third number: 86
Largest number: 86.0
Smallest number: 23.0
[Program finished]
12
Q12. write a Python program to accept two integers and print their sum.
# CODE OF PROGRAM
# Accepting two integers from the user
num1 = int(input("Enter the first integer: "))
num2 = int(input("Enter the second integer: "))
# Calculating the sum
sum_result = num1 + num2
# Printing the sum
print("The sum of", num1, "and", num2, "is:", sum_result)
# OUTPUT OF PROGRAM
Enter the first integer: 34
Enter the second integer: 65
The sum of 34 and 65 is: 99
[Program finished]
13
Q13. Write a Python program that accepts the radius of a circle and prints its area.
# CODE OF PROGRAM
import math
# Accepting the radius from the user
radius = float(input("Enter the radius of the circle: "))
# Calculating the area of the circle using the formula: A = π * r^2
area = math.pi * (radius ** 2)
# Printing the area
print("The area of the circle with radius", radius, "is:", area)
# OUTPUT OF PROGRAM
Enter the radius of the circle: 45
The area of the circle with radius 45.0 is: 6361.725123519331
[Program finished]
14
Q14. Write a Python program to accept the length and width of a rectangle and compute
its perimeter and area.
# CODE OF PROGRAM
#Accepting the length and width from the user
length = float(input("Enter the length of the rectangle: "))
width = float(input("Enter the width of the rectangle: "))
# Calculating the perimeter of the rectangle:
P = 2 * (length + width)
perimeter = 2 * (length + width)
# Calculating the area of the rectangle:
A = length * width
area = length * width
# Printing the results
print("The perimeter of the rectangle is:", perimeter)
print("The area of the rectangle is:", area)
# OUTPUT OF PROGRAM
Enter the length of the rectangle: 12
Enter the width of the rectangle: 11
The perimeter of the rectangle is: 46.0
The area of the rectangle is: 132.0
[Program finished]
15
Q15.Write a Python program to compute simple interest for a given Principal amount,
time and rate of interest.
# CODE OF PROGRAM
# Accepting the principal amount, time, and rate of interest from the user
principal = float(input("Enter the principal amount: "))
time = float(input("Enter the time (in years): "))
rate_of_interest = float(input("Enter the rate of interest (as a percentage): "))
# Calculating simple interest using the formula:
SI = (P * T * R) / 100
simple_interest = (principal * time * rate_of_interest) / 100
# Printing the calculated simple interest
print("The simple interest is:", simple_interest)
# OUTPUT OF PROGRAM
Enter the principal amount: 100000
Enter the time (in years): 10
Enter the rate of interest (as a percentage): 8
The simple interest is: 80000.0
[Program finished]
16
Q16.Write a Python program to find whether a given number is even or odd?
# CODE OF PROGRAM
# Accepting a number from the user
number = int(input("Enter a number: "))
# Checking if the number is even or odd
if number % 2 == 0:
print(number, "is an even number.")
else:
print(number, "is an odd number.")
# OUTPUT OF PROGRAM
Enter a number: 45
45 is an odd number.
[Program finished]
17
Q17. Write a Python program to find the largest among three numbers.
# CODE OF PROGRAM
# Function to find the largest among three numbers
def find_largest(num1, num2, num3):
largest = max(num1, num2, num3)
return largest
# Input three numbers
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))
# Call the function and display the result
largest_number = find_largest(num1, num2, num3)
print("The largest number is:", largest_number)
# OUTPUT OF PROGRAM
Enter the first number: 23
Enter the second number: 45
Enter the third number: 87
The largest number is: 87.0
[Program finished]
18
Q18. Write a Python program to perform arithmetic calculation. This program accepts
two operands and an operator then displays the calculated result.
# CODE OF PROGRAM
# Function to perform arithmetic calculation
def perform_calculation(operand1, operand2, operator):
if operator == '+':
result = operand1 + operand2
elif operator == '-':
result = operand1 - operand2
elif operator == '*':
result = operand1 * operand2
elif operator == '/':
if operand2 != 0:
result = operand1 / operand2
else:
return "Cannot divide by zero"
else:
return "Invalid operator"
return result
# Input two operands and an operator
operand1 = float(input("Enter the first operand: "))
operand2 = float(input("Enter the second operand: "))
operator = input("Enter the operator (+, -, *, /): ")
# Call the function and display the result
calculation_result = perform_calculation(operand1, operand2, operator)
print("Result:", calculation_result)
# OUTPUT OF PROGRAM
Enter the first operand: 45
Enter the second operand: 76
Enter the operator (+, -, *, /): -
Result: -31.0
[Program finished]
19
Q19. Write a Python program to check whether a given year is a leap year or not.
# CODE OF PROGRAM
# Function to check if a year is a leap year
def is_leap_year(year):
# Leap years are divisible by 4
if year % 4 == 0:
# If divisible by 100, it must also be divisible by 400
if year % 100 == 0:
if year % 400 == 0:
return True
else:
return False
else:
return True
else:
return False
# Input a year
year = int(input("Enter a year: "))
# Check if it's a leap year and display the result
if is_leap_year(year):
print(year, "is a leap year.")
else:
print(year, "is not a leap year.")
# OUTPUT OF PROGRAM
Enter a year: 3045
3045 is not a leap year.
[Program finished]
20
Q20. Write a Python program to print tables of a given number.
# CODE OF PROGRAM
# Function to print the multiplication table of a given number
def print_multiplication_table(number):
print(f"Multiplication table for {number}:")
for i in range(1, 11):
print(f"{number} x {i} = {number * i}")
# Input a number
num = int(input("Enter a number to print its multiplication table: "))
# Call the function to print the multiplication table
print_multiplication_table(num)
# OUTPUT OF PROGRAM
Enter a number to print its multiplication table: 12
Multiplication table for 12:
12 x 1 = 12
12 x 2 = 24
12 x 3 = 36
12 x 4 = 48
12 x 5 = 60
12 x 6 = 72
12 x 7 = 84
12 x 8 = 96
12 x 9 = 108
12 x 10 = 120
[Program finished]
21
Q21. Write a Python Program to print first n Natural numbers and their sum.
# CODE OF PROGRAM
# Function to print first n natural numbers and their sum
def print_natural_numbers_and_sum(n):
sum_of_numbers = 0
print(f"First {n} natural numbers:")
for i in range(1, n + 1):
print(i, end=" ")
sum_of_numbers += i
print("\nSum of first", n, "natural numbers:", sum_of_numbers)
# Input the value of n
n = int(input("Enter the value of n: "))
# Call the function to print natural numbers and their sum
print_natural_numbers_and_sum(n)
# OUTPUT OF PROGRAM
Enter the value of n: 5
First 5 natural numbers:
12345
Sum of first 5 natural numbers: 15
[Program finished]
22
Q22. Write a Python Program to accept two integers X and N, compute XN
# CODE OF PROGRAM
# Function to compute X to the power of N
def compute_power(x, n):
result = x ** n
return result
# Input two integers X and N
x = int(input("Enter the value of X: "))
n = int(input("Enter the value of N: "))
# Call the function and display the result
result = compute_power(x, n)
print(f"{x} raised to the power of {n} is: {result}")
# OUTPUT OF PROGRAM
Enter the value of X: 6
Enter the value of N: 9
6 raised to the power of 9 is: 10077696
[Program finished]
23
Q23. Write a Python Program to calculate the factorial of a given number using a while
loop.
# CODE OF PROGRAM
# Function to calculate the factorial of a given number
def calculate_factorial(number):
factorial = 1
while number > 1:
factorial *= number
number -= 1
return factorial
# Input a number
num = int(input("Enter a number to calculate its factorial: "))
# Call the function and display the result
result = calculate_factorial(num)
print(f"The factorial of {num} is: {result}")
# OUTPUT OF PROGRAM
Enter a number to calculate its factorial: 34
The factorial of 34 is: 295232799039604140847618609643520000000
[Program finished]
24
Q24. Write a Python program to check whether a given number is equal to the sum of
the cubes of its digits.
# CODE OF PROGRAM
# Function to check if a number is equal to the sum of the cubes of its digits
def is_sum_of_cubes_equal_to_number(number):
temp = number
sum_of_cubes = 0
# Calculate the sum of cubes of digits
while temp > 0:
digit = temp % 10
sum_of_cubes += digit ** 3
temp //= 10
# Check if the sum of cubes is equal to the original number
return sum_of_cubes == number
# Input a number
num = int(input("Enter a number: "))
# Check and display the result
if is_sum_of_cubes_equal_to_number(num):
print(f"{num} is equal to the sum of the cubes of its digits.")
else:
print(f"{num} is not equal to the sum of the cubes of its digits.")
# OUTPUT OF PROGRAM
Enter a number: 45
45 is not equal to the sum of the cubes of its digits.
[Program finished]