Python Program - Find Smallest Number among three Numbers
Three numbers x, y and z are given and the smallest number among these three numbers can be found out using below methods.
Method 1: Using If statement
In the example below, only if conditional statements are used.
def smallest(x, y, z): if x <= y and x <= z: min = x if y <= x and y <= z: min = y if z <= x and z <= y: min = z print("Smallest number among", x,",", \ y,"and",z,"is: ",min) smallest(100, 50, 25) smallest(50, 50, 25)
The above code will give the following output:
Smallest number among 100 , 50 and 25 is: 25 Smallest number among 50 , 50 and 25 is: 25
Method 2: Using If-else statement
It can also be solved using If-else conditional statements.
def smallest(x, y, z): if x <= y and x <= z: min = x elif y <= x and y <= z: min = y else: min = z print("Smallest number among", x,",", \ y,"and",z,"is: ",min) smallest(100, 50, 25) smallest(50, 50, 25)
The above code will give the following output:
Smallest number among 100 , 50 and 25 is: 25 Smallest number among 50 , 50 and 25 is: 25
Method 3: Using Nested If-else statement
The above problem can also be solved using nested if-else conditional statements.
def smallest(x, y, z): if x <= y: if x <= z: min = x else: min = z else: if y <= z: min = y else: min = z print("Smallest number among", x,",", \ y,"and",z,"is: ",min) smallest(100, 50, 25) smallest(50, 50, 25)
The above code will give the following output:
Smallest number among 100 , 50 and 25 is: 25 Smallest number among 50 , 50 and 25 is: 25
Method 4: Using Short hand If-else statement
Short hand if-else conditional statements can also be used here.
def smallest(x, y, z): min = x if x < y else y if y < z else z print("Smallest number among", x,",", \ y,"and",z,"is: ",min) smallest(100, 50, 25) smallest(50, 50, 25)
The above code will give the following output:
Smallest number among 100 , 50 and 25 is: 25 Smallest number among 50 , 50 and 25 is: 25
Recommended Pages
- Python Program - To Check Prime Number
- Python Program - Bubble Sort
- Python Program - Selection Sort
- Python Program - Maximum Subarray Sum
- Python Program - Reverse digits of a given Integer
- Python - Swap two numbers
- Python Program - Fibonacci Sequence
- Python Program - Insertion Sort
- Python Program - Find Factorial of a Number
- Python Program - Find HCF of Two Numbers
- Python Program - To Check Whether a Number is Palindrome or Not
- Python Program - To Check Whether a String is Palindrome or Not
- Python Program - Heap Sort
- Python Program - Quick Sort
- Python - Swap Two Numbers without using Temporary Variable
- Python Program - To Check Armstrong Number
- Python Program - Counting Sort
- Python Program - Radix Sort
- Python Program - Find Largest Number among Three Numbers
- Python Program - Print Floyd's Triangle