Page 304 - Touhpad Ai
P. 304
11. Write a program to accept the marks of 5 students from the user. Calculate and display the average marks of the
students.
# Initialize the total marks to 0
total_marks = 0
# Accept marks from the user and calculate the total
for i in range(5):
mark = float(input("Enter the marks:"))
total_marks += mark
# Calculate the average
average_marks = total_marks / 5
# Display the average
print("The average marks of the 5 students is:",average_marks)
Output:
Enter the marks: 45.6
Enter the marks: 67.5
Enter the marks: 78.5
Enter the marks: 80
Enter the marks: 77
The average marks of the 5 students is: 69.72
12. Write a menu driven program to display the sum or product of three numbers as per user’s choice.
# Display the menu and get user's choice
print("Menu:")
print("1. Sum of three numbers")
print("2. Product of three numbers")
choice = int(input("Enter your choice (1 or 2): "))
# Get three numbers from the user
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
num3 = int(input("Enter the third number: "))
# Calculate and display the result based on the user's choice
if choice == 1:
sum = num1 + num2 + num3
print("The sum is:",sum)
elif choice == 2:
prod = num1 * num2 * num3
print("The product is:",prod)
else:
print("Invalid choice. Please enter 1 or 2.")
Output:
Menu:
1. Sum of three numbers
2. Product of three numbers
Enter your choice (1 or 2): 2
Enter the first number: 23
Enter the second number: 6
Enter the third number: 7
The product is: 966
302 Touchpad Artificial Intelligence - XI

