# Min And Max Numbers Using A User-defined Function In Python

(Last Updated On: 21/11/2022)

## Python Program to Find Min And Max’s Numbers Using A User-defined Function

```# Min and Max Numbers in Python using a user-defined function

# we are taking a number from user as input
# entered value will be converted to int from string
number1 = int(input('Enter First number : '))
number2 = int(input('Enter Second number : '))
number3 = int(input('Enter Third number : '))

def largest(num1, num2, num3):
if (num1 > num2) and (num1 > num3):
largest_num = num1
elif (num2 > num1) and (num2 > num3):
largest_num = num2
else:
largest_num = num3
print("The largest of the three numbers is : ", largest_num)

def smallest(num1, num2, num3):
if (num1 < num2) and (num1 < num3):
smallest_num = num1
elif (num2 < num1) and (num2 < num3):
smallest_num = num2
else:
smallest_num = num3
print("The smallest of the three numbers is : ", smallest_num)

largest(number1, number2, number3)
smallest(number1, number2, number3)```

#### Output:

``````Enter First number : 25
Enter Second number : 50
Enter Third number : 75
The largest of the three numbers is :  75
The smallest of the three numbers is :  25``````

### Let’s see how this program works

#### Our Approach is:

• Check if the first element is smaller than the second and third. If yes, then print it.
• Else check if the second element is smaller than the first and third. If yes, then print it.
• Else third is the smallest element and print it.

Here at first, we have defined 3 variables and assigned values that we are taking from the user. We can also define static variables, but dynamic is useful in big projects.

In this program, we have called two functions largest and smallest, and pass the same variable which we have accepted from the user. Let’s check the 1st function, which is the largest(); this function accepts 3 parameters.

Inside this function, we have an if condition which checks if num1 is greater than number 2 and num1 is greater than num3, which means if both the conditions are true, then num1 is greater than the other two.

If the above condition is not satisfied, then we will check if num2 is greater than num1 and num3. If this condition is also false, then num3 is the largest, and this function will print the largest number with a message. Also, in the case of the smallest number, this function also accepts three numbers, and inside this function, we have a condition to check.

If num1 is less than the other two numbers. If this condition is true, then we will assign the value of variable num1 to a variable smallest_num. In case if this condition is false, then the program will move to the elif condition and check further conditions and print the smallest number among all 3 with a message. Let’s run this program and enter numbers 25, 50, and 75. See, we have our output max number as 75 and minimum number as 25.