C++ Program to Find Largest Number Among Three Numbers


The largest number among three numbers can be found using if statement multiple times. This is given in a program as follows −

Example

 Live Demo

#include <iostream>
using namespace std;
int main() {
   int a = 5 ,b = 1 ,c = 9;
   if(a>b) {
      if(a>c)
      cout<<a<<" is largest number";
      else
      cout<<c<<" is largest number";
   }else {
      if(b>c)
      cout<<b<<" is largest number";
      else
      cout<<c<<" is largest number";
   }
   return 0;
}

Output

9 is largest number

In the above program, firstly, a is compared to b. If a is greater than b, then it is compared to c. If it is greater than c as well, that means a is the largest number and if not, then c is the largest number.

if(a>b) {
   if(a>c)
   cout<<a<<" is largest number";
   else
   cout<<c<<" is largest number";
}

If a is not greater than b, that means b is greater than a. Then b is compared to c. If it is greater than c, that means b is the largest number and if not, then c is the largest number.

else {
   if(b>c)
   cout<<b<<" is largest number";
   else
   cout<<c<<" is largest number";
}

Updated on: 24-Jun-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements