C++ program to find the largest of two numbers

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

Prerequisite

Program: Find Largest of Two Numbers

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

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

Output

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

Description and working this program

  • Take two numbers from the user and store them in “number1” and “number2”.
  • Check condition if the first number is greater than the second than largest= number1
  • If this condition is not true then largest = number2
  • Print largest on the screen.