Write a program to display a menu driven facility to find the greatest of two numbers,three numbers and four numbers
Question
Write a program to display a menu driven facility to find the greatest of
- two numbers,
- three numbers
- and four numbers.
Solution
Sure, here is a simple Python program that provides a menu-driven facility to find the greatest of two, three, and four numbers.
def find_greatest(numbers):
return max(numbers)
while True:
print("\nMenu")
print("1. Find greatest of two numbers")
print("2. Find greatest of three numbers")
print("3. Find greatest of four numbers")
print("4. Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
print("Greatest number is", find_greatest([num1, num2]))
elif choice == 2:
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
num3 = int(input("Enter third number: "))
print("Greatest number is", find_greatest([num1, num2, num3]))
elif choice == 3:
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
num3 = int(input("Enter third number: "))
num4 = int(input("Enter fourth number: "))
print("Greatest number is", find_greatest([num1, num2, num3, num4]))
elif choice == 4:
break
else:
print("Invalid choice. Please choose a valid option.")
This program first defines a function find_greatest
that takes a list of numbers and returns the maximum number. It then enters a loop where it displays a menu to the user and asks for their choice. Depending on the user's choice, it asks for the appropriate number of inputs, finds the greatest number using the find_greatest
function, and prints the result. If the user chooses to exit, it breaks out of the loop. If the user enters an invalid choice, it prints an error message and continues with the next iteration of the loop.
Similar Questions
Develop a Java program to identify the largest among four numbers (32, 29,34, 21) using appropriate control statement and operators.
Construct a Pseudocode to take three numbers and print largest and smallest among them.
Write a C++ code to find largest among 4 numbers using the concept call by reference.
Write a function int max_of_four(int a, int b, int c, int d) which reads four arguments and returns the greatest of them.
The program must accept four integers a, b, c and d as input. The program must solve the equation b * c - (a + d) and print it as the output.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.