C++ program to find the smallest of two numbers

In this example, you will learn a c++ program to find the smallest value between two numbers. This program takes to numbers from the user and displays the smallest value. If the user enters 4 and 7 programs find the smallest from both which is 4.

Prerequisite

To understand this program perfectly, you should have the following C++ programming knowledge.

Program: Find the Smallest of Two Numbers

// C++ program to find smallest of two numbers
#include<iostream>
using namespace std;

int main()
{
int number1, number2, smallest ;
cout<<"Enter first number: ";
cin>>number1;
cout<<"Enter second number: ";
cin>>number2;
    
if(number1<number2)
{
smallest=number1;
}
else
{
smallest=number2;
}
cout<<"Smallest of the two number is: "<<smallest;
return 0;
}

Output

Enter first number: 4
Enter second number: 7
Smallest of the two number is: 4

Description and working of this program

  • Take two numbers from the user and store them in “number1” and “number2”.
  • Condition if the first number is smaller than the second than smallest= number1
  • If the above condition is not true then smallest = number2
  • Print smallest on the screen.