C++ Program to Find Biggest of Three Numbers.

Here we will understand how to find the largest number among the three numbers using C++ code. 

Examples:
Input: int a = 5, b = 8, c = 2
Output: The Biggest number among the three is 8

Input: int a = 54, b = 21, c = 9
Output: The Biggest number among the three is 54

There are multiple ways to solve this problem:
  1. Using if statement.
  2. Using if-else statement.
  3. Using Ternary Operator.
1. Algorithm using if statement.
  • Take three input variables from the user and store them in three different variables a, b, and c.
  • Check condition if a >= b and a >= c, if both conditions are true then print "The Biggest number among three is a. 
  • Check condition if b >= a and b >= c, if both conditions are true then print "The Biggest number among three is b. 
  • Check condition if c >= a and c >= b, if both conditions are true then print "The Biggest number among three is c.  
  • Stop the Program.
//C++ Code to find Biggest among three numbers
#include<iostream>
using namespace std;

int main(){
    int a, b, c;

    cout << "Enter three numbers: ";
    cin >> a >> b >> c;

    if(a >= b && a >= c)
       cout<<"The Biggest number among the three is "<<a;

    if(b >= a && b >= c)
       cout<<"The Biggest number among the three is "<<b;

    if(c >= a && c >= b)
       cout<<"The Biggest number among the three is "<<c;
             
    return 0;                    
}
Output:
Enter three numbers: 10 34 54
The Biggest number among the three is 54

2. Algorithm using if-else statement
This algorithm is a little better than the previous one because here we are not going to check the remaining conditions once we find our first true condition.  
  • Take three input variables from the user and store them in three different variables a, b, and c.
  • Check first if-condition, a >= b and a >= c, if conditions are true then print "The Biggest number among three is a" and end the program.
  • Check the next else-if condition b >= a and b >= c, if conditions are true then print "The Biggest number among three is b" and end the program.
  • If both the above conditions fail then without checking any further conditions you can declare the third variable as the largest variable.
  • End the Program.
//C++ Code to find Biggest among three numbers
//using if-else 
#include<iostream>
using namespace std;

int main(){
    int a, b, c;

    cout << "Enter three numbers: ";
    cin >> a >> b >> c;

    if(a >= b && a >= c)
       cout<<"The Biggest number among the three is "<<a;
    else if(b >= a && b >= c)
       cout<<"The Biggest number among the three is "<<b;
    else
       cout<<"The Biggest number among the three is "<<c;
             
    return 0;                    
}
Output:
Enter three numbers: -5 4 5
The Biggest number among the three is 5

3. Algorithm using Ternary Operator.
The ternary operator is a shorter way of writing if-else conditions and based on the results of the condition a particular block of code executes.
  • (condition) ? expression 1 : expression 2
Here, if the condition is true then expression 1 will get executed and if the condition is false then expression 2 will get executed.
//C++ Code to find Biggest among three numbers 
//using ternary operator
#include<iostream>
using namespace std;

int main(){
    int a, b, c;
    int ans;
    cout << "Enter three numbers: ";
    cin >> a >> b >> c;

    //Ternary Operator
    ans = (a >= b ? (a >= c ? a : c) : (b >= c ? b : c));
    
    cout<<"The Biggest number among the three is "<<ans;
             
    return 0;                    
}
Output:
Enter three numbers: 5 3 12
The Biggest number among the three is 12

⚡ Please share your valuable feedback and suggestion in the comment section below or you can send us an email on our offical email id ✉ algolesson@gmail.com. You can also support our work by buying a cup of coffee ☕ for us.

Similar Posts

No comments:

Post a Comment


CLOSE ADS
CLOSE ADS