You are on page 1of 13

Q.1 Input a welcome. message and ,Display it.

Ans :-

def display_welcome_message():

# Input a welcome message

welcome_message = input("Enter your welcome message: ")

# Display the entered welcome message

print("Welcome Message:", welcome_message)

# Call the function to execute the program

display_welcome_message()

Q.2 Inpul two numbers and display the larger/ smaller number?
Ans –
def compare_numbers():
# Input two numbers
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))

# Compare and display the larger number


if num1 > num2:
print(f"{num1} is larger than {num2}.")
print(f"{num2} is smaller than {num1}.")
elif num2 > num1:
print(f"{num2} is larger than {num1}.")
print(f"{num1} is smaller than {num2}.")
else:
print("Both numbers are equal.")

# Call the function to execute the program


compare_numbers()
Q.3 Input three numbers and display the largest/ smallest number ?
Ans :-
def compare_three_numbers():
# Input three numbers
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))

# Find and display the largest number


largest_number = max(num1, num2, num3)
smallest_number = min(num1, num2, num3)

print(f"The largest number is: {largest_number}")


print(f"The smallest number is: {smallest_number}")

# Call the function to execute the program


compare_three_numbers()
 Q 4. Generate the following patterns using nested loops

Pattern-1 Pattern-2 Pattern •3


* 12345 A
1234 AB
*** 123 ABC
**** 12 ABCD
***** 1 ABCDE
Ans :-
1. def pattern_1():
for i in range(1, 6):
for j in range(i):
print("*", end="")
print()

# Call the function to generate Pattern-1


pattern_1()
2. def pattern_2():
for i in range(5, 0, -1):
for j in range(6 - i):
print(j + 1, end="")
print()

# Call the function to generate Pattern-2


pattern_2()

3. def pattern_3():
for i in range(65, 70):
for j in range(65, i + 1):
print(chr(j), end="")
print()

# Call the function to generate Pattern-3


pattern_3()

Q.5 Determine whether a number is a perfect number, an Armstrong


number or a palindrome
Ans :-
def is_perfect_number(number):
# Check if the number is a perfect number
divisors_sum = sum([divisor for divisor in range(1, number) if number %
divisor == 0])
return divisors_sum == number

def is_armstrong_number(number):
# Check if the number is an Armstrong number
num_digits = len(str(number))
digit_sum = sum(int(digit) ** num_digits for digit in str(number))
return digit_sum == number

def is_palindrome(number):
# Check if the number is a palindrome
return str(number) == str(number)[::-1]
def main():
# Input a number
number = int(input("Enter a number: "))

# Check and display the results


if is_perfect_number(number):
print(f"{number} is a perfect number.")
else:
print(f"{number} is not a perfect number.")

if is_armstrong_number(number):
print(f"{number} is an Armstrong number.")
else:
print(f"{number} is not an Armstrong number.")

if is_palindrome(number):
print(f"{number} is a palindrome.")
else:
print(f"{number} is not a palindrome.")

# Call the main function to execute the program


main()
Q.6 Input a number and check If the number is a prime or composite
number

Ans :-

def is_prime(number):

# Check if the number is a prime number

if number < 2:

return False

for i in range(2, int(number**0.5) + 1):

if number % i == 0:

return False

return True

def main():
# Input a number

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

# Check and display whether the number is prime or composite

if is_prime(number):

print(f"{number} is a prime number.")

else:

print(f"{number} is a composite number.")

# Call the main function to execute the program

main()

Q.7 Display the terms of a Fibonacci series


Ans :-
def fibonacci_series(n):
# Generate and display the Fibonacci series up to the nth term
fib_series = [0, 1]
for i in range(2, n):
next_term = fib_series[-1] + fib_series[-2]
fib_series.append(next_term)

return fib_series

def main():
# Input the number of terms for the Fibonacci series
n = int(input("Enter the number of terms for the Fibonacci series: "))

# Display the Fibonacci series


result = fibonacci_series(n)
print(f"The Fibonacci series up to {n} terms is: {result}")

# Call the main function to execute the program


main()
Q.8 Compute the greatest common divisor and least common multiple of
two integers_

Ans :-

def find_gcd(x, y):

# Compute the GCD of two numbers using Euclidean algorithm

while y:

x, y = y, x % y

return abs(x)

def find_lcm(x, y):

# Compute the LCM of two numbers using the formula: LCM(x, y) = |x * y| / GCD(x, y)

return abs(x * y) // find_gcd(x, y)

def main():

# Input two integers

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

num2 = int(input("Enter the second integer: "))

# Compute and display the GCD and LCM

gcd_result = find_gcd(num1, num2)

lcm_result = find_lcm(num1, num2)

print(f"The GCD of {num1} and {num2} is: {gcd_result}")

print(f"The LCM of {num1} and {num2} is: {lcm_result}")


# Call the main function to execute the program

main()

Q.9 Count and display the number of vowels, consonants, uppercase.


lowercase characters in string

Ans :-

def count_characters(input_string):

# Initialize counters

vowels_count = 0

consonants_count = 0

uppercase_count = 0

lowercase_count = 0

# Define vowels

vowels = "AEIOUaeiou"

# Iterate through each character in the string

for char in input_string:

# Check for vowels

if char in vowels:

vowels_count += 1

# Check for consonants

elif char.isalpha():

consonants_count += 1

# Check for uppercase letters

elif char.isupper():
uppercase_count += 1

# Check for lowercase letters

elif char.islower():

lowercase_count += 1

# Display the counts

print(f"Number of vowels: {vowels_count}")

print(f"Number of consonants: {consonants_count}")

print(f"Number of uppercase characters: {uppercase_count}")

print(f"Number of lowercase characters: {lowercase_count}")

def main():

# Input a string

input_string = input("Enter a string: ")

# Call the function to count and display characters

count_characters(input_string)

# Call the main function to execute the program

main()

Q.10 Input a string and determine whether it is a palindrome or not; convert


the case of characters in a string.
Ans :-
def is_palindrome(input_string):
# Remove spaces and convert to lowercase for case-insensitive palindrome check
cleaned_string = ''.join(input_string.split()).lower()
# Check if the cleaned string is a palindrome
return cleaned_string == cleaned_string[::-1]

def convert_case(input_string):
# Convert the case of characters in the string
return input_string.swapcase()

def main():
# Input a string
input_string = input("Enter a string: ")

# Check and display whether it is a palindrome


if is_palindrome(input_string):
print("The entered string is a palindrome.")
else:
print("The entered string is not a palindrome.")

# Convert and display the case of characters


converted_string = convert_case(input_string)
print(f"The string with converted case: {converted_string}")

# Call the main function to execute the program


main()
Q.11 - Find the largest/smallest number h a list/tuple

Ans :-
Finding the Largest Number in a List or Tuple:
def find_largest(numbers):
# Find the largest number in the list or tuple
largest_number = max(numbers)
return largest_number

# Example with a list


numbers_list = [34, 12, 56, 78, 23]
largest_in_list = find_largest(numbers_list)
print(f"The largest number in the list is: {largest_in_list}")

# Example with a tuple


numbers_tuple = (45, 67, 12, 89, 34)
largest_in_tuple = find_largest(numbers_tuple)
print(f"The largest number in the tuple is: {largest_in_tuple}")

Finding the Smallest Number in a List or Tuple:


def find_smallest(numbers):
# Find the smallest number in the list or tuple
smallest_number = min(numbers)
return smallest_number

# Example with a list


numbers_list = [34, 12, 56, 78, 23]
smallest_in_list = find_smallest(numbers_list)
print(f"The smallest number in the list is: {smallest_in_list}")

# Example with a tuple


numbers_tuple = (45, 67, 12, 89, 34)
smallest_in_tuple = find_smallest(numbers_tuple)
print(f"The smallest number in the tuple is: {smallest_in_tuple}")

Q.12 input a list of numbers and swap elemenis at ilhe even locatlun with the
elemenis at the odd location.
Ans:-
def swap_elements_at_odd_even_locations(input_list):
# Check if the list has even length, if not, ignore the last element
if len(input_list) % 2 == 1:
input_list = input_list[:-1]

# Swap elements at even and odd locations


for i in range(0, len(input_list)-1, 2):
input_list[i], input_list[i+1] = input_list[i+1], input_list[i]

return input_list

def main():
# Input a list of numbers
input_numbers = [int(x) for x in input("Enter a list of numbers separated by spaces:
").split()]

# Swap elements at even and odd locations and display the result
swapped_list = swap_elements_at_odd_even_locations(input_numbers)
print(f"The list after swapping elements at even and odd locations: {swapped_list}")

# Call the main function to execute the program


main()
Q.13 Inpul a list/tuple of elemems, search for a given element in the
list/tuple
Ans :-
def search_element(container, target):
# Search for the target element in the list or tuple
if target in container:
return f"The element {target} is found at index {container.index(target)}."
else:
return f"The element {target} is not found in the list or tuple."

def main():
# Input a list or tuple of elements
input_container = eval(input("Enter a list or tuple of elements (e.g., [1, 2, 3]): "))

# Input the element to search for


search_target = eval(input("Enter the element to search for: "))

# Search for the element and display the result


result = search_element(input_container, search_target)
print(result)

# Call the main function to execute the program


main()
Q.14 Create a dictionary with the roll number. name ard marks of n students
in a class and display the names of students who have marks above 75
Ans :
def students_above_75(marks_dict):
# Filter students with marks above 75
above_75_students = {roll: name for roll, (name, marks) in marks_dict.items() if
marks > 75}
return above_75_students

def main():
# Input the number of students
n = int(input("Enter the number of students: "))

# Create a dictionary with roll numbers, names, and marks


student_dict = {}
for i in range(n):
roll_number = input(f"Enter roll number for student {i + 1}: ")
name = input(f"Enter name for student {i + 1}: ")
marks = float(input(f"Enter marks for student {i + 1}: "))
student_dict[roll_number] = (name, marks)

# Display the names of students with marks above 75


above_75_students = students_above_75(student_dict)
print("\nStudents with marks above 75:")
for roll, (name, marks) in above_75_students.items():
print(f"Roll Number: {roll}, Name: {name}")

# Call the main function to execute the program


main()

You might also like