computer assignment
computer assignment
1
~I N D E X~
1. WAP to make a calculator having ‘+’ ,’-’ ,’*’, ‘/’, ‘//’, ‘%’ as operators
Input the numbers and operator from the user. Page no- 05
2. WAP to input the name and current age of the user and check in
which year the user will turn 100 years old. Page no- 07
3. WAP to input a text from the user and count the total number of
alphabets, spaces,words,uppercases,lowercases,numbers,special
characters in the text. Page no- 08
6. WAP to input a list from the user and search an inputted element in
the list.Display its positive and negative index along with its occurence.
Page no- 16
10. Compute the greatest common divisor and least common multiple
of two integers. Page no- 24
2
11. Write a Python program that takes user input to create two
tuples, t1 and t2. Swap the content of the tuples, and display both
the original and swapped tuples. Page no- 25
13. Write a Python program that allows the user to input integers to
create a tuple. Prompt the user to enter a specific integer for frequency
checking. Page no- 27
17. WAP to input names of ‘n’ employees and their salary detail.
Page no- 32
18. Write a Python program that calculates the area and perimeter for a
chosen geometric shape. Page no- 34
3
1.WAP to make a calculator having ‘+’ ,’-’ ,’*’, ‘/’, ‘//’, ‘%’ as operators
Input the numbers and operator from the user.
else:
# Display an error message for invalid operations
print("Invalid operation. Please enter a valid operator (+, -
/,//, %).")
4
O/p
5
2.WAP to input the name and current age of the user and check in which
year the user will turn 100 years old.
{Write an appropriate msg regarding the same}
O/p
6
3.WAP to input a text from the user and count the total number of
alphabets, spaces,words,uppercases,lowercases,numbers,special
characters in the text.
{Write an appropriate msg regarding the same}
print('1 - Space')
print('2 - Digit')
print('3 - Alphabet')
print('4 - Uppercase')
print('5 - Lowercase')
print('6 - Special characters')
print('7 - Words')
7
if choice == 1:
print('Number of spaces:', s)
elif choice == 2:
print('Number of digits:', d)
elif choice == 3:
print('Number of alphabets:', a)
elif choice == 4:
print('Number of uppercase characters:', u)
elif choice == 5:
print('Number of lowercase characters:', l)
elif choice == 6:
print('Number of special characters:', e)
elif choice == 7:
print('Number of words:', len(sp)) #length of splitted
words
else:
print("Invalid choice. Enter a no. b\w '1,2,3,4,5,6,7'")
O/p
8
9
10
4.Generate the following patterns using nested loops:
Pattern-1
for i in range(1, 6):
print("*" * i)
O/p
Pattern-2
for i in range(5, 0, -1):
for j in range(1, i + 1):
print(j, end="")
print()
O/p
11
Pattern-3
for i in range(1, 6):
for j in range(1, i + 1):
print(chr(64 + j), end="")
print()
O/p
12
5.WAP to input ur marks of five subjects display its average, total marks of
5 subjects, percentage and grade acc. To the following table:
13
elif percent>=71:
print("Your grade is B1")
elif percent>=61:
print("Your grade is B2")
elif percent>=51:
print("Your grade is C1")
elif percent>=41:
print("Your grade is C2")
elif percent>=33:
print("Your grade is D")
else :
print("Congratulations,You have failed ur examination:)")
O/p
14
6.WAP to input a list from the user and search an inputted element in the
list.
Display its positive and negative index along with its occurence.
l = [] # Empty list
range_list = int(input('Enter the range for your list: '))
for i in range(len(l)):
if l[i] == num:
ind.append(i)
count += 1
15
O/p
16
7.Generate the following patterns using nested loops:
for i in range(1,6):
num=1
for j in range(6,0,-1):
if j>i:
print(' ',end='')
else:
print('*',end='')
print()
O/p
17
8.Enter a string. Do you want to perform any case-related operations on
the string? Choose from the following options:
Uppercase,Lowercase,Capitalise,Title,Swapcase,Reverse:
print("1. Uppercase")
print("2. Lowercase")
print("3. Capitalize")
print("4. Title")
print("5. Swapcase")
print("6. Reverse")
print("7. Exit")
if choice == "1":
result = user_input.upper()
elif choice == "2":
result = user_input.lower()
elif choice == "3":
result = user_input.capitalize()
elif choice == "4":
result = user_input.title()
elif choice == "5":
result = user_input.swapcase()
elif choice == "6":
result = user_input[::-1]
elif choice == "7":
print("Exiting program. No changes made.")
else:
print("Invalid choice. No changes made.")
else:
print("The input does not contain alphabets.")
18
O/p
19
20
9.Enter a temperature value. Choose a conversion:
1. Celsius to Fahrenheit
2. Fahrenheit to Celsius
3. Kelvin to Celsius
4. Celsius to Kelvin
Enter the corresponding number for your choice:
if choice == "1":
converted_temperature = (temperature * 9/5) + 32
print(str(temperature) + " Celsius is equal to " +
str(converted_temperature) + " Fahrenheit.")
elif choice == "2":
converted_temperature = (temperature - 32) * 5/9
print(str(temperature) + " Fahrenheit is equal to " +
str(converted_temperature) + " Celsius.")
elif choice == "3":
converted_temperature = temperature - 273.15
print(str(temperature) + " Kelvin is equal to " +
str(converted_temperature) + " Celsius.")
elif choice == "4":
converted_temperature = temperature + 273.15
print(str(temperature) + " Celsius is equal to " +
str(converted_temperature) + " Kelvin.")
else:
print("Invalid choice. Please enter a valid number.")
21
O/p
22
10.Compute the greatest common divisor and least common multiple of
two integers.
# Compute GCD
a, b = num1, num2
while b:
a, b = b, a % b
result_gcd = a
# Compute LCM
result_lcm = abs(num1 * num2) // result_gcd
O/p
23
11.Write a Python program that takes user input to create two tuples, t1
and t2. Prompt the user to enter values for each tuple, swap the
content of the tuples, and display both the original and swapped
tuples.
t1 = t2 = tuple()
n = int(input("Number of values in the first tuple: "))
for i in range(n):
a = int(input('Enter number: '))
t1 = t1 + (a,)
print('First tuple:', t1)
n = int(input("Number of values in the second tuple: "))
for i in range(n):
a = int(input('Enter number: '))
t2 = t2 + (a,)
print('Second tuple:', t2)
t1, t2 = t2, t1
print('After Swapping:')
print('First tuple:', t1)
print('Second tuple:', t2)
O/p
24
12.Write a program to search an element in a tuple.
t = ()
n = int(input("Size of the tuple: "))
for i in range(n):
a = int(input('Enter the number: '))
t = t + (a,)
print('Tuple:', t)
25
13.Write a Python program that allows the user to input integers to create a
tuple. After creating the tuple, prompt the user to enter a specific integer
for frequency checking. Output the frequency of the entered integer in
the tuple.
t = ()
n = int(input("Size of the tuple: "))
for i in range(n):
a = int(input('Enter the number: '))
t = t + (a,)
print('Tuple:', t)
if c == 0:
print(element, 'not found in the tuple')
else:
print(element, 'found', c, 'times in the tuple')
O/p
26
14.Write a program to enter names of employees and their salaries as input
and store them in a dictionary.
employee_salary_dict = {}
while True:
employee_name = input("Enter employee name: ")
salary = int(input("Enter employee salary: "))
employee_salary_dict[employee_name] = salary
O/p
27
15.Write a program to enter name and percentage in a dictionary of n
number of students and display the information.
student_records = {}
number_of_students = int(input('Enter the no. of students: '))
i = 1
28
16.Write a program to enter name and percentage in a dictionary of n
number of students and display the information.
if choice.lower() == 'w':
# Counting word frequency
words = input_string.split()
for item in words:
if item in frequency:
frequency[item] += 1
else:
frequency[item] = 1
elif choice.lower() == 'l':
# Counting letter frequency
for char in input_string:
if char.isalpha():
if char in frequency:
frequency[char] += 1
else:
frequency[char] = 1
character_count += 1
elif choice.lower() == 'c':
# Counting character frequency
for char in input_string:
if char in frequency:
frequency[char] += 1
else:
frequency[char] = 1
character_count += 1
else:
print("Invalid choice! Please enter 'w', 'l', or 'c'.")
# Displaying the frequency and character count
print("Frequency:")
for key, value in frequency.items():
print(key, '->', value)
print("Character count:", character_count)
29
O/p
30
17.Write a program to input names of ‘n’ employees and their salary details
like basic pay (bp), house rent ( hra ) , conveyance allowance(ca).
calculate the total salary of each employee.
[total salary = basic pay+house rent + conveyance allowance]
employee_data = {}
n = int(input("Enter the number of employees: "))
employee_data[name] = {
"Basic Pay": basic_pay,
"House Rent": house_rent,
"Conveyance Allowance": conveyance_allowance,
"Total Salary": total_salary
}
31
O/p
32
18.Write a Python program that calculates the area and perimeter for a
chosen geometric shape. The program should provide the user with
options to select a shape (Circle, Square, Rectangle, Triangle) by
entering the corresponding number. After selecting a shape, the user will
input the required dimensions, and the program will display the
calculated area and perimeter.
import math
def circle_area(radius):
return math.pi * radius**2
def circle_perimeter(radius):
return 2 * math.pi * radius
def square_area(side):
return side**2
def square_perimeter(side):
return 4 * side
33
def main():
choice = int(input("Choose a shape:\n1. Circle\n2.
Square\n3. Rectangle\n4. Triangle\nEnter the corresponding
number: "))
if choice == 1:
radius = float(input("Enter the radius of circle: "))
print("Area:", circle_area(radius))
print("Perimeter:", circle_perimeter(radius))
elif choice == 2:
side = float(input("Enter the side length of the
square: "))
print("Area:", square_area(side))
print("Perimeter:", square_perimeter(side))
elif choice == 3:
length = float(input("Enter the length of the
rectangle: "))
width = float(input("Enter the width of the rectangle:
"))
print("Area:", rectangle_area(length, width))
print("Perimeter:", rectangle_perimeter(length,
width))
elif choice == 4:
base = float(input("Enter the base length of the
triangle: "))
height = float(input("Enter the height of the
triangle: "))
print("Area:", triangle_area(base, height))
side1 = float(input("Enter the length of side 1: "))
side2 = float(input("Enter the length of side 2: "))
side3 = float(input("Enter the length of side 3: "))
print("Perimeter:", triangle_perimeter(side1, side2,
side3))
else:
print("Invalid choice. Please enter a no. b/w 1 & 4.")
main()
34
O/p
35
19.Wap to roll 2 dices simultaneously, if the digits are same on both the
dices then declare the user as winner.
import random
def roll_dice():
return random.randint(1, 6)
def main():
print("Welcome to the Dice Rolling Game!")
while True:
input("Press Enter to roll two dice...")
result1 = roll_dice()
result2 = roll_dice()
if result1 == result2:
print("Congratulations! You are the winner!")
else:
print("Better luck next time!")
main()
36
O/p
37
20. WAP to Check if a list is a palindrome (reads the same forward and
backward).
def is_palindrome():
input_list = []
n = int(input('Enter the no of elements: '))
for i in range(n):
num = int(input('Enter a no.: '))
input_list.append(num)
reversed_list = list(reversed(input_list))
if input_list == reversed_list:
print('The entered sequence is a palindrome.')
else:
print('The entered sequence is not a palindrome.')
is_palindrome()
O/p
38
○THANK○
○YOU○
39