Programming Tutorials Hub

Learn Python with simple code, clear explanations, and examples

Python program to find the largest among three numbers using nested-if

📄 Code


n1 = input("Enter first number:")
n2 = input("Enter second number:")
n3 = input("Enter third number:")

n1 = int(n1)
n2 = int(n2)
n3 = int(n3)

if n1 > n2:
    if n1 > n3:
        print(n1,"is largest")
    else:
        print(n3,"is largest")
elif n2 > n3:
    print(n2,"is largest")
else:
    print(n3,"is largest")
        
💡 Output
Enter first number:1
Enter second number:3
Enter third number:2
3 is largest

Enter first number:2
Enter second number:1
Enter third number:3
3 is largest

Enter first number:3
Enter second number:1
Enter third number:2
3 is largest
        
🧠 Code Explanation
To find the largest among three numbers using nested-if, following operations and performed.

Let's take an example by taking numbers 1, 3 and 2.

n1 = input("Enter first number:")
n2 = input("Enter second number:")
n3 = input("Enter third number:")

The input() function is used here to get the user input at runtime.

n1 = input("Enter first number:")
The first input() function is used to get the 'first' number from the user and it will be assigned to the variable 'n1' as str(string) object.
number  = "1"

n2 = input("Enter second number:")
The second input() function is used to get the 'second' number from the user and it will be assigned to the variable 'n2' as str(string) object.
number  = "3"

n3 = input("Enter third number:")
The third input() function is used to get the 'third' number from the user and it will be assigned to the variable 'n3' as str(string) object.
number  = "2"


n1 = int(n1)
n2 = int(n2)
n3 = int(n3)

n1 = int(n1)
The number "1" which is stored as string is now get converted into integer using int() function and is assigned to the variable 'n1'.
n1 = 1

n2 = int(n2)
The number "3" which is stored as string is now get converted into integer using int() function and is assigned to the variable 'n2'.
n2 = 3

n3 = int(n3)
The number "2" which is stored as string is now get converted into integer using int() function and is assigned to the variable 'n3'.
n3 = 2

if n1 > n2:
    if n1 > n3:
        print(n1,"is largest")
    else:
        print(n3,"is largest")
elif n2 > n3:
    print(n2,"is largest")
else:
    print(n3,"is largest")

if the value stored in variable 'n1' is greater than that of 'n2', and, if the value of 'n1' is greater than 'n3', then value of variable 'n1' is largest.

if the value stored in 'n1' is greater than that of 'n2', and, if the value of 'n1' is not greater than 'n3', then value of variable 'n3' is largest.

if the value stored in 'n1' is not greater than that of 'n2', and, if the value of 'n2' is greater than 'n3', then value of variable 'n2' is largest.

if the value stored in 'n1' is not greater than that of 'n2', and, if the value of 'n2' is not greater than 'n3', then value of variable 'n3' is largest.

n1 = 1
n2 = 3
n3 = 2

if n1 > n2:
	=> if 1 > 3: which is False. Therefore the body of if statement will not get executed and program control goes to elif statement.

elif n2 > n3:
	=> elif 3 > 2: which is True. Therefore the body of the elif statement will get executed. ie. 'print(n2,"is largest")' statement will get executed and the output will be '3 is largest'.