Write a Python program to cap a number within the inclusive range specified by the given boundary values x and y


  1. If num falls within the range (a, b), return num
  2. Otherwise, return the nearest number in the range

This is a Python program that takes three numbers as input from the user and then returns the maximum of the minimum number and the minimum of the other two numbers.

The program first prompts the user to enter three numbers using the input() function, and the int() function is used to convert the inputs to integers. The input values are then assigned to the variables num, a, and b.

The max() function is used to find the maximum of the minimum number (min(num, max(a, b))) and the minimum of the other two numbers (min(a, b)).

Here, min(num, max(a, b)) finds the minimum of num and the maximum of a and b. Then, min(a, b) finds the minimum of a and b. Finally, the max() function finds the maximum of the two minimum values.

Source Code

num = int(input("Enter the Number :"))
a = int(input("Enter the A Number :"))
b = int(input("Enter the B Number :"))
print(max(min(num, max(a, b)), min(a, b)))

Output

Enter the Number :3
Enter the A Number :45
Enter the B Number :23
23

Example Programs