×

Python program to find the greatest among two numbers

Python program to find the greatest among two numbers

We have many approaches to get the largest number among the two numbers, and here we will discuss a few of them. This python program needs the user to enter two different values. Next, Python will compare these numbers and print the greatest among them, or both are equal.

Example:

Input: A = 5, B = 6  Output: B is greater than A

Source Code

The following is the source code that print the greatest amongst them or both numbers are equal:

 #Take two number from user to compare                     
 num1 = int(input("Enter first number: "))      
 num2 = int(input("Enter second number: ")) 
 #Compare both the number 
 if num1 >= num2:   
     if num1 == num2:    
         print("Both numbers are equal.")       
     else:  
         print("Fisrt number is greater than the second number.")
 else:   
     print("Second number is greater than the First number.") 

Output

Case 1:

Python program to find the greatest among two numbers

Case 2:

Python program to find the greatest among two numbers

Case 3:

Python program to find the greatest among two numbers

Explanation: In the above code, we have asked a user to take two integer values as an input and store them in a separate variable. After taking the inputs, it will compare both the numbers and check that which number is greater or both the number is equal by using if....else statement. After performing the comparison, the final result will be printed on the screen.

Largest of Two Numbers using Arithmetic Operator

In this Python programs, we are using a Minus operator to find the greatest of Two Numbers:

#Program to find the greatest among two number using operators  
#Take two number from user to compare    
num1 = int(input("Enter first number: "))  
num2 = int(input("Enter second number: "))   
#Compare both the number 
 if(num1 - num2 > 0): 
print("{0} is largest than {1}".format(num1, num2)) 
elif(num2 - num1 > 0):     
print("{0} is largest than {1}".format(num2, num1)) 
else:     print("Both numbers are Equal")

Output

Here is the result:

Python program to find the greatest among two numbers

Related Topics

Python id() function

Python id() function The id() function in Python returns an id for the specified object where all the objects in has its own unique id. Syntax id(object) Parameter object: This parameter represents any object, String, Number, List,...

1 minute read.

Attributes in python

In this article, we shall learn about attributes in python. Classes are a mix of data and functions, which in reality mean attributes and methods respectively. Typically, the body of a...

3 minutes read.

Python JSON

In this tutorial, we will learn about JSON in the Python programming language. We will focus on its features, Guidelines to be kept in mind while writing its syntax, the...

3 minutes read.

Arithmetic Expressions in Python

What is Python Expression? Expressions are collections of operands and operators. Python expressions are translated by the Python interpreter into some value or outcome. In Python, an expression is made up...

13 minutes read.

End Parameter in python

print(): The python print() function prints the program’s output to the output screen. The output can be an integer value, string value or other value. Syntax: print(“hi”) Output: hi will be displayed on the output...

3 minutes read.

Closest Pair of Points in Python

We are given an array of n points in the plane, and our task is to find the pair of points in the array that are the closest to each...

3 minutes read.

Base Case in Recursive function python

In this article, we will learn about Python's base case in a recursive function. Before learning this, let’s first understand what recursion is in Python and the use of recursion in...

6 minutes read.

Python System Requirements

Introduction As we know, Python is a popular programming language usually used to write scripts for operating systems. It’s handy adequate for utilizing in web development and application design. In this article,...

2 minutes read.

Composition in Python

Composition in Python Composition is one of the important concepts of Object-oriented programming (OOPs). Composition basically enables us for creating complex types objects by combining other types of objects in the...

6 minutes read.

Anaconda in Python 3

What is Anaconda in Python 3? Anaconda is an open-source package combining Python and R programming languages used for data analytics and processing data. It consists of a huge number of...

4 minutes read.

Reverse a Number in Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

4 minutes read.

How to Iterate a List in Python

As we know, a List is one of the four unique data structures available in Python; in this article, we will deep dive into understanding iterating through a list and...

3 minutes read.

Python program to find Fibonacci series

Python program to find Fibonacci series A Fibonacci series is an integer sequence of 0, 1, 1, 2, 3, 5, 8.... We can identify the Fibonacci series as any number sequence...

2 minutes read.

Python str() function

Python str() function The str() function in Python converts the specified value into a string. Syntax class str(object='')           or class str(object=b'', encoding='utf-8', errors='strict') Parameter object:  This parameter represents any object to convert the given...

1 minute read.

Python Hash Table

An Introduction to Hashing A technique which used to identify a particular object uniquely from a set of similar objects is known as Hashing. Some real-life examples of hashing implementations include: A...

9 minutes read.

Captcha Code in Python with Example

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

6 minutes read.

Python Switch Case

What is a Switch Case Statement? In the programming languages, a switch statement is a logical loop or syntax that tests the variable's value and compares it with multiple cases. Once...

3 minutes read.

Python Pass Statement

The pass statement is a null statement. The difference between pass and comment is that comment is ignored by the interpreter, whereas pass is not. The pass statement is typically used...

3 minutes read.

Reverse a String in Python

Python is an object-oriented high-level programming language. Python has dynamic semantics and has high-level built-in data structures which support dynamic typing and dynamic binding. Python provides rapid development. It has...

4 minutes read.

Python String count() method

Python String count() method The string.count() method returns the number of times a specified value appears in the string. Syntax string.count(sub[, start[, end]]) Parameter sub: This parameter represents a substring to be searched. start: This parameter...

1 minute read.