C++ program to find the smallest of two numbers

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

Program: Find the Smallest of Two Numbers in C++

// 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

cpp program to find the smallest of two numbers

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 the smallest value on the screen.

Recommended Articles

C++ If-else Statements

C++ Operators

Scroll to Top