Codeforcoding

Program to find largest number among three numbers in Cpp

Program to find the largest number among three numbers in the Cpp

In this program, we will discuss a simple concept of program to find the largest number among three numbers in the Cpp programming language.

In this topic, we learn how to find the biggest number from given three numbers. we can use the operator in C++ language to find the biggest number of this program.

program to find largest number among three numbers in Cpp
find largest number among three numbers

Using if statements to find the largest number

Among integer numbers

Program 1

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int num1, num2,num3; //declare the variables
    cout<<"Enter the first number to compare: ";
    cin>>num1;//get input from user for num1
    cout<<"Enter the second number to compare: ";
    cin>>num2;//get input from user for num2
    cout<<"Enter the third number to compare: ";
    cin>>num3;//get input from user for num3

    if(num1>=num2 && num1>=num3){
         cout<<"\nLargest number is:"<<num1;
    }//num1 compare num 2 and num 3
    if(num2>=num1 && num2>=num3){
        cout<<"\nLargest number is:"<<num2;
    }//num2 compare num1 and num3
    if(num3>=num1 && num3>=num2){
         cout<<"\nLargest number is:"<<num3;
    }//num3 compare num1 and num2

   getch();
    return 0;
}

When the above code is compiled and executed, it produces the following results

Enter the first number to compare: 45
Enter the second number to compare: 87
Enter the third number to compare: 34

Largest number is: 87

 

Among floating point numbers

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    double num1, num2,num3; //declare the variables
    cout<<"Enter the first number to compare: ";
    cin>>num1;//get input from user for num1
    cout<<"Enter the second number to compare: ";
    cin>>num2;//get input from user for num2
    cout<<"Enter the third number to compare: ";
    cin>>num3;//get input from user for num3

    if(num1>=num2 && num1>=num3){
         cout<<"\nLargest number is:"<<num1;
    }//num1 compare num 2 and num 3
    if(num2>=num1 && num2>=num3){
        cout<<"\nLargest number is:"<<num2;
    }//num2 compare num1 and num3
    if(num3>=num1 && num3>=num2){
         cout<<"\nLargest number is:"<<num3;
    }//num3 compare num1 and num2

   getch();
    return 0;
}

When the above code is compiled and executed, it produces the following results

Enter the first number to compare: 3.4
Enter the second number to compare: 8.4
Enter the third number to compare: 5.6

Largest number is: 8.4

 

Using nested if statements to find the largest number

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
    int num1,num2,num3; //declare the variables
    cout<<"Enter the first number to compare: ";
    cin>>num1;//get input from user for num1
    cout<<"Enter the second number to compare: ";
    cin>>num2;//get input from user for num2
    cout<<"Enter the third number to compare: ";
    cin>>num3;//get input from user for num3

    if(num1>=num2){//compare num1 and num2
            if(num1>=num3){//compare num1 and num3
         cout<<"\nLargest number is:"<<num1;
            }
            else{
                cout<<"\nLargest number is:"<<num3;
            }
    }
    else{
        if(num2>=num1){//compare num2 and num1
            cout<<"\nLargest number is:"<<num2;
        }
        else{
            cout<<"\nLargest number is:"<<num3;
        }

    }
   getch();
    return 0;
}

When the above code is compiled and executed, it produces the following results

Enter the first number to compare: 56
Enter the second number to compare: 90
Enter the third number to compare: 54 

Largest number is: 90

 

Using  if -else statements to find the largest number

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
    int num1,num2,num3; //declare the variables
    cout<<"Enter the first number to compare: ";
    cin>>num1;//get input from user for num1
    cout<<"Enter the second number to compare: ";
    cin>>num2;//get input from user for num2
    cout<<"Enter the third number to compare: ";
    cin>>num3;//get input from user for num3

    if(num1>=num2 && num1>=num3){
         cout<<"\nLargest number is: "<<num1;
    }//num1 compare num 2 and num 3
    else if(num2>=num1 && num2>=num3){
         cout<<"\nLargest number is: "<<num2;
    }//num2 compare num1 and num3
    else{
         cout<<"\nLargest number is: "<<num2;
    }

   getch();
    return 0;
}

When the above code is compiled and executed, it produces the following results

Enter the first number to compare: 56 
Enter the second number to compare: 90 
Enter the third number to compare: 95 

Largest number is: 95

 

Suggested for you

If statements in C++ language

Nested if statements in C++ language

Operator in C++ language

Data type in C++ language

Variable in C++ language

 

If statements in C language

Nested if statements in C language

Operator in C language

Data type in C language

Variable in C language

 

Similar post

 largest number of three number in C

 largest number of three number in C++

 largest number of three number in Python

 largest number of three number in C using function

 largest number of three number in C++ using function

 largest number of three number in Python using function

 largest number of three number in Java using method

Program to find largest among three numbers in C
Program to find largest number among three numbers in Java
Exit mobile version