×

Python Program to Find the Greatest Among Three Numbers

Python Program to find the greatest among three numbers

We have many approaches to get the largest of three numbers, and here we will discuss a few of them. This python program needs the user to enter three numbers. Next, Python will compare these numbers and print the greatest among them.

Input: A = 4, B = 6, C = 8  Output: C is the greatest

Method 1: By using Python Elif Statement

#Program to find the greatest among three   
#Take three number from user to compare 
num1 = int(input("Enter first number: ")) 
num2 = int(input("Enter second number: "))
num3 = int(input("Enter third number: "))
#compare the number 
 if (num1 > num2 and num1 > num3): 
 print("{0} is Greater Than both {1} and {2}". format(num1, num2, num3)) 
elif (num2 > num1 and num2 > num3):           
print("{0} is Greater Than both {1} and {2}". format(num2, num1, num3)) 
elif (num3 > num1 and num3 > num2):          
 print("{0} is Greater Than both {1} and {2}". format(num3, num1, num2))
 else:      
#Print the result  
print("Either any two values or all the three values are equal")

Output

CASE 1:

greatest among three numbers

CASE 2:

greatest among three numbers

Explanation: In this program, a user will be asked to enter three numbers. This number will be stored in a separate variable. Now, we will compare all three numbers against each other using if...elif...else statement. After the comparison, the greatest among the three numbers will returns and prints on the screen.

Method 2: By using Nested If Statement

This program uses the nested IF statement to find the Largest of Three numbers in Python:

#Program to find the greatest among three     
 #Take three number from user to compare  num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: ")) 
num3 = int(input("Enter third number: ")) 
#compare the number 
 if (num1-num2 > 0) and (num1-num3 > 0):    
 print("{0} is Greater Than both {1} and {2}". format(num1, num2, num3)) 
else:    
 if(num2 - num3 > 0): 
print("{0} is Greater Than both {1} and {2}". format(num2, num1, num3))   
  else:       
  print("{0} is Greater Than both {1} and {2}". format(num3, num1, num2))

Output

Here is the output:

greatest among three numbers

Related Topics

Python String lstrip() method

Python String lstrip() method The string. lstrip () method in Python returns a copy of the string with leading characters removed (based on the string argument passed). Syntax string.lstrip([chars]) Parameter chars(optional): This parameter represents a...

1 minute read.

Python Read CSV file

The CSV stands for “comma-separated value,” which is defined as a simple file format that is used to store data into a tabular form such as a database or spreadsheet. It is...

7 minutes read.

Python Program to Check Leap Year

Python Program to Check Leap Year Leap year: We all know that each year has 365 or 366 days. But whenever a year has 366 days, then the year is said...

2 minutes read.

Python Skyline

Python Programming Language: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a...

3 minutes read.

Python String swapcase() method

Python String swapcase() method The string.swapcase() method in Python returns a copy of the string with uppercase characters converted to lowercase and vice versa. Syntax string.swapcase() Parameter NA Return This method returns a copy of the string...

1 minute read.

Python Matrix Multiplication

One of the most fundamental mathematical structures, matrices are used often in many disciplines, including mathematics, physics, engineering, computer science, etc. For example, matrices and associated operations (multiplication and addition) are...

8 minutes read.

Python String zfill() method

Python String zfill() method The string.zfill() method in Python returns a copy of the string while adding the zeros (0) at the beginning of the string, until it reaches the specified...

1 minute read.

Anytree Python

Python's anytree module is frequently used within data science-related software. Anytree has a Tolerant License, a Construct File that is public, no errors, no risks, and excellent support. One may...

3 minutes read.

How to Fix an EOF Error in Python?

Introduction An EOF (End-of-File) error occurs when a program tries to read beyond the end of a file or a stream, causing it to return an error message. It can happen...

8 minutes read.

PyDev with Python IDE

What is IDE? Integrated Development Environment is the abbreviation of IDE. It is a coding tool that automates code editing, finding errors and testing. IDE combines all the developer tools into...

3 minutes read.

Python pow() Function

Python pow() Function The pow() function in Python return the parameter ‘x’ to the power ‘y’ and if the parameter ‘z’ is present, it returns x to the power y, modulo z (computed more efficiently than pow(x, y) % z). Syntax pow(x, y[, z]) Parameter x: This parameter represents the base...

1 minute read.

Python Classification

Identification and grouping of items or concepts into specified categories this process is known the classification. Data can be separated and sorted in data management according to predetermined criteria for...

6 minutes read.

Why learn Python?

All things considered, learning python would be extraordinary, it'll acquaint you with the universe of dynamic programming dialects, if you're somebody who has had a semester of involvement with C. Python...

4 minutes read.

Python isinstance() function

Python isinstance() function The isinstance() function in Python returns a Boolean value ‘True’ if the given object is of the specified type, otherwise it returns False. Syntax isinstance(object, classinfo) Parameter object: It is a required parameter which represents an object. classinfo: This...

1 minute read.

Append key Value to Dictionary in Python

The Python dictionary is one of the built-in data types. Elements of dictionaries are key-value pairs. In Python, there are numerous ways to add dictionaries. Let's examine some of the...

9 minutes read.

Python program to check whether a given number is prime or not

Python program to check whether a given number is prime or not A positive integer greater than 1 is called a prime number if it is divisible by one and number...

1 minute read.

How to create a DataFrames in Python

How to create a DataFrames in Python Data Frame is a data structure in which data is stored in tabular form. They can also be referred as two-dimensional collection of data. Various...

5 minutes read.

How to Download all Modules in Python

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Python String isupper() method

Python String isupper() method The String.isupper() method in Python returns a boolean value ‘true’ if all cased characters in the string are uppercase else for any other value is returns false. Syntax String.isupper() Parameter NA Return This...

2 minutes read.

What is online python free IDE

An IDE is an acronym that stands for “Integrated development environment.” It is a software environment that has a software development package that consists of the code editor and builds...

4 minutes read.