Introduction:
This Python program takes user input for n numbers and stores them in a tuple. It then finds and prints the maximum and minimum numbers from the tuple.
Code:
#Program to input n numbers from the user. Store these numbers
#in a tuple. Print the maximum and minimum number from this tuple.
numbers = tuple() #create an empty tuple 'numbers'
n = int(input("How many numbers you want to enter?: "))
for i in range(0,n):
num = int(input())
#it will assign numbers entered by user to tuple 'numbers'
numbers = numbers +(num,)
print('\nThe numbers in the tuple are:')
print(numbers)
print("\nThe maximum number is:")
print(max(numbers))
print("The minimum number is:")
print(min(numbers))
Logic:
- Create an empty tuple ‘numbers’.
- Take input from the user for the number of numbers (n) they want to enter.
- Use a for loop to iterate ‘n’ times and take input for each number from the user.
- Assign the entered numbers to the tuple ‘numbers’ using the tuple concatenation operation.
- Print the numbers stored in the tuple.
- Find and print the maximum and minimum numbers from the tuple using the max() and min() functions.
Output:
How many numbers you want to enter?: 5
10
15
20
25
30
>>
>> The numbers in the tuple are:
>> (10, 15, 20, 25, 30)
>>
>> The maximum number is:
>> 30
>> The minimum number is:
>> 10