×

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

EDA in Python

The EDA is the exploratory data analysis; the data scientists mainly use the EDA to understand the main features of the data quickly, the variables in python and the relationship...

6 minutes read.

Alexa Python

The cloud-based voice assistant renders text into speech in a female voice. Alexa is a virtual assistant released by Amazon. Using itself as a system for home automation, Alexa can...

4 minutes read.

How To Install Python In Ubuntu

How To Install Python In Ubuntu Ubuntu is free and open-source software and it is an essential part of the Linux distribution. It is a popular operating system developed by Canonical. If we...

3 minutes read.

__init__ in python

Every programmer will enclose to object-oriented programming (OOP) these days. As we know, that python is the latest and most modern programming language; python supports to implementation of object-oriented philosophy....

5 minutes read.

Python Project Ideas

One of the most widely used programming languages today is Python. This pattern appears set to continue through 2023 and beyond. Therefore, working on some current Python project ideas is the...

10 minutes read.

Python pynmea2

Define Pynmea2 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...

4 minutes read.

How to Import Files in 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 vs R

Python: - Python is a popular high-level, general-purpose programming language. Python (the most recent version is Python 3) is a programming language used in the software industry for website development, machine...

3 minutes read.

Python locals() function

Python locals() function The locals() function in Python updates and returns a dictionary representing the current local symbol table. Syntax locals() Parameter NA Return This function returns the local symbol table as a dictionary or returns free...

1 minute read.

Accessing Key-value in Dictionary in Python

A Python word reference is an assortment of key-esteem matches where each key is related to worth. Worth in the key-esteem pair can be a number, a string, a rundown,...

5 minutes read.

Python TCP Server

The TCP server library is also known as the socket library. Socket programming is the method of connecting two nodes through a network to form communication between two nodes. In...

3 minutes read.

How to reverse a string in python

How to reverse a string in python A Brief About Strings- The String is a data type in Python that has a sequence of characters. This series of characters are represented in...

4 minutes read.

Python Redis Example

Introduction: Redis, representing Distant Word reference Server, is a kind of key-esteem NoSQL server. Redis is intended to help circle capacity for determination, holding information after power is stopped, and stores information...

5 minutes read.

Nested for Loop in Python

"Loop" is one of the foundation concepts to learn programming in any language. Nested loops are significant steps in solving many types of problems, ranging from basic to complex scenarios....

9 minutes read.

Python program to perform the arithmetic operation

Python program to perform the arithmetic operation This program will write a code to perform some basic arithmetic operations like addition, subtraction, multiplication, exponent, modulus, and division. Here we first need...

2 minutes read.

Compound Interest GUI Calculator using PyQt5 in Python

GUI: One of the most significant factors that increased the usability of computer and digital technologies for common, less tech-savvy users is likely the development and widespread adoption of GUIs. GUIs...

6 minutes read.

Python Random Module

The tutorial for the Python random module demonstrates how to produce pseudo-random integers in Python. Random Number Generator (RNG) The RNG (random number generator) generates a series of values with no discernible...

8 minutes read.

How to make API calls in Python

Information is extremely critical these days since it drives applications and organizations. It is subsequently critical to figure out how to get this information to serve your application. In fundamental...

4 minutes read.

Difference between Module and Package in Python

The main difference between the module and the package in Python is that the module can be a simple file in Python that contains the different functions and collection of...

4 minutes read.

Python Compiler

What is Compiler? The compiler is mainly a program used to convert the source code into the machine or binary code. The source code is generally a computer program written using...

6 minutes read.