Skip to main content

Write a python program to find largest of 3 numbers by using IF Statement

Description​

This Python code determines the largest among three numbers (a, b, and c) provided by user input. It uses if statements to compare the values and displays a message indicating which of the three numbers is the largest.

Code​

program3a.py
a=int(input("Enter value of a"))
b=int(input("Enter value of b"))
c=int(input("Enter value of c"))
if a>b and a>c:
print("a is largest")
if b>a and b>c:
print("b is largest")
if c>a and c>b:
print("c is largest")

Explanation of above code​

  • The Python code is intended to identify the largest value among three numbers, a, b, and c, based on user input.
  • It initiates by using the input function to gather numerical values for each of the variables: a, b, and c.
  • The input values are subsequently converted into integers using the int function, making them suitable for numerical comparisons.
  • The code employs a series of if statements to compare these input values and determine the largest number among them.
  • The first if statement checks if 'a' is greater than both 'b' and 'c'. If true, it prints 'a is largest.'
  • The second if statement assesses whether 'b' is greater than both 'a' and 'c' and, if so, it outputs 'b is largest.'
  • The third if statement examines whether 'c' is the largest number by comparing it with 'a' and 'b'. If it is the largest, it displays 'c is largest.'
  • Consequently, the code provides the user with the identification of the largest value among the three input numbers, demonstrating the use of conditional statements and user input in Python..

Learn more​

Reference​