Press "Enter" to skip to content

C++ : Input Three Numbers and Find the Largest Among them

Here C++ program find the biggest among three given numbers. There are many solution for this problem. YOC describes three of them below.

In all programs, user reads three numbers in to variables a, b, and c respectively.

Solution 1:

#include <iostream>
using namespace std;
int main()
{
    int a,b,c,biggest;
    cout << "Type any three numbers :";
    cin>>a>>b>>c;
    if(a>b &amp;&amp; a>c)
        cout<<"biggest is "<<a;
    else if(b>a &amp;&amp; b>c)
        cout<<"biggest is "<<b;
    else
    cout<<"biggest is "<< c;
    return 0;
}

This is the simplest way. Here we compare first number with second and third numbers simultaneously. if it is true, then first number is definitively the biggest. Similarly second number is compared together with remaining numbers and if it is true second number will be the biggest, else third number will be the largest.

Solution 2:

#include <iostream>
using namespace std;
int main()
{
    int a,b,c,biggest;
    cout << "Type any three numbers :";
    cin>>a>>b>>c;
    if(a>b)
    {
      if(a>c)
        biggest = a;
      else
        biggest = c;
    }
    else if(b>c)
        biggest = b;
    else
        biggest = c;
    cout<<"biggest is "<< biggest;
    return 0;
}

In above program, if the first number is bigger than second, then compare with third number, if it is still bigger, then which the biggest of them. If the first ‘if’ block is false, then compare second number with third number. if second number is bigger than third one, then second number is the biggest, else third number is largest of them.

Solution 3:

#include <iostream>
using namespace std;
int main()
{
    int a,b,c,biggest;
    cout << "Type any three numbers :";
    cin>>a>>b>>c;
    biggest = a;
    if(b>biggest)
        biggest = b;
    if(c>biggest)
        biggest = c;
        cout<<"biggest is "<< biggest;
    return 0;
}

This is an alternative solution to the solution solution 2. Here a number is considered as the biggest and compare with remaining numbers. If a number is biggest than previous biggest reputed number, then that number will have the reputation of biggest.

Sample Output:

Biggest among three given numbers C++ Program

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *