Write a program to read two numbers and find the largest and smallest number between them

In this python program, we will take two numbers from user and display larger and smaller number in python, which can compare these two numbers and print larger and smaller number depending upon the user-entered input.

Also, we will discuss how to check equality of both number in python

Identify larger and smaller number in python

Let us see how we can compare and display larger and smaller number in python 

Program Logic:

  • Take two number as input from user using input method
  • Use if-else block to check which number is larger or smaller
  • Display larger and smaller number using print statement

Below is implementation code / Source code

# python program to find smaller and larger number #take two number from user num1 = int(input("Enter first number :")) num2 = int(input("Enter second number :")) if (num1 > num2): print(num1,"is larger than",num2) elif (num1 == num2): print(num1,"is equal to",num2) else : print(num1,"is smaller than",num2)

Explanation:

Here, we ask the user to enter first and second number of their choice. If the first number is greater than second number then it will display ” first number is larger than second number” else it will display “first number is smaller than second number”. If both numbers are equal then we it will display “first number is equal to second number”

Below is output of above code

>>> %Run 'smaller and larger number.py' Enter first number :23 Enter second number :34 23 is smaller than 34 >>> %Run 'smaller and larger number.py' Enter first number :34 Enter second number :33 34 is larger than 33 >>> %Run 'smaller and larger number.py' Enter first number :23 Enter second number :23 23 is equal to 23 >>>

Snapshot of executable code

Write a program to read two numbers and find the largest and smallest number between them
Snapshot of python program to find smaller and larger number

Here we will learn about how to find and print smallest between any two given number (by user at run-time) with and without using user-defined function. At last, we will also use Ternary operator to find and print smallest between the two numbers.

Find Smallest of Two Numbers in C

Here is the program that will ask from the user to enter any two number to find out and print the smallest one as shown here:

#include<stdio.h> #include<conio.h> int main() { int a, b, small; printf("Enter any two number: "); scanf("%d%d", &a, &b); if(a<b) small=a; else small=b; printf("\nSmallest of the two number is: %d", small); getch(); return 0; }

The program was written under Code::Blocks IDE, therefore after successful build and run, here is the first snapshot of the sample run of above program:

Write a program to read two numbers and find the largest and smallest number between them

Updated on 05-Mar-2021 11:00:04