C++ Program to find Largest of Two Numbers

Write a C++ program to find the largest number from the given two numbers. In the below written C++ program, we used the Else if statement to find the largest of two. If (x > y), the first if condition check whether x is greater than y. If true, x is greater than y. Next, else if(y > x) check whether y is greater than x. If true, y is greater than x. If both the above conditions fail, both are equal.

#include<iostream>

using namespace std;

int main()
{
	int x, y;
	
	cout << "Please enter the Two Different Number  = ";
	cin >> x >> y;
	
	if(x > y)
	{
    	cout << x << " is Greater Than " << y;  
	}         
	else if(y > x)
	{
		cout << y << " is Greater Than " << x;  
  	}
  	else
  	{
  		cout << "Both are Equal";
	}
 	return 0;
}
C++ Program to find Largest of Two Numbers 1

C++ Program to find Largest of Two Numbers Example 2

The first if condition checks whether both the x and y are equal or not. If it is false, then we used the conditional operator ((x > y) ? x : y) to return the largest of two. 

#include<iostream>

using namespace std;

int main()
{
	int x, y, largest;
	
	cout << "Please enter the Two Different Number  = ";
	cin >> x >> y;
	
	if(x == y)
	{
		cout << "\nBoth are Equal";
	}  
	else
	{
		largest = (x > y) ? x : y;
		cout << "\nThe Largest value of two Numbers = " << largest;
	}
		
 	return 0;
}
Please enter the Two Different Number  = 89 77

The Largest value of two Numbers = 89

Let me try the other value in this C++ program.

Please enter the Two Different Number  = 55 98

The Largest value of two Numbers = 98