Table of contents
1.
Introduction
2.
Problem Statement
3.
Some Examples
4.
Program to find the largest number among three numbers in C++
4.1.
Method 1: Using comparison operators
4.2.
Method 2: Using the max() function
5.
FAQs
6.
Key Takeaways
Last Updated: Mar 27, 2024

Program to find the largest number among three numbers

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

This blog will discuss the solutions to this problem on how to find the largest number among three numbers in C++. We will code these solutions in the C++ language. We will discuss different approaches to solving this problem.

To learn more about C++ programs, visit the C++ guided path.

Problem Statement

Write a program to find the largest number among three numbers in C++.

Some Examples

Input

a = 45

b = 7

c = 11

Output

Largest No. = 45

Explanation

Largest number among the given numbers i.e. a = 45, b = 7, c = 11, is 45.
 

Input

a = 7

b = 3

c = 7

Output

Largest No. = 7

Explanation

Largest number among the given numbers i.e. a = 7, b = 3, c = 7, is 7.

Program to find the largest number among three numbers in C++

Now we will solve this problem on how to find the largest number among three numbers in C++ using many different methods. These methods are as follows:

Method 1: Using comparison operators

First, let's start with a straightforward program to find the largest number among three numbers in C++ using in-built comparison operators. A brief description of these operators is listed below in the table.

Operator

Description

Example

Greater than( > ) Returns true if the LHS is greater than the RHS; false otherwise. 4 > 3 returns  true
Greater than or equals to( >= ) Returns true if the LHS is greater than or equals the RHS; false otherwise. 3 >= 3 returns true
Less than( < ) Returns true if the LHS is less than the RHS; false otherwise. 4 < 3 returns false
Less than or equals to( <= ) Returns true if the LHS is less than or equals the RHS; false otherwise. 3 <= 3 returns true

 

Now, let's see the implementation of the above approach.

Code:

#include <iostream>
using namespace std;


// Function to find the largest number among three numbers
int findLargest(int a, int b, int c)
{
    // Find the largest number among three numbers
    if(a>b) {
        if(a>=c) {
            return a;
        } else{
            return c;
        }
    } else{
        if(b >= c) {
            return b;
        } else {
            return c;
        }
    }
}
 
// Driver Code
int main()
{
 
    // Function call
    cout<<"Largest No. = "<<findLargest(45, 7, 11 )<<endl;
    cout<<"Largest No. = "<<findLargest(7, 3, 7)<<endl;
    return 0;
}
You can also try this code with Online C++ Compiler
Run Code

 

The above code will find the largest number among three numbers using comparison operators and nested if-else statements. The output of the above code is displayed below:

Output

Time Complexity: O(1) because we find the largest number among three numbers using if-else statements and a comparison operator in constant time.

Space Complexity: O(1) because no extra variables are used.

Method 2: Using the max() function

Here, simply, we will use the max() Function. The max() function returns the maximum among two numbers. We can use this function multiple times to compare more numbers. 

Syntax for max() function: max(a , b);

We can implement this approach as mentioned below:

Code:

#include <iostream>
using namespace std;


// Function to find the largest number among three numbers
int findLargest(int a, int b, int c)
{
    // Find the largest number among three numbers
    int ans = max(a, b);
    ans = max(ans, c);
    return ans;
}
 
// Driver Code
int main()
{
    
    // Function call
    cout<<"Largest No. = "<<findLargest(45, 7, 11 )<<endl;
    cout<<"Largest No. = "<<findLargest(7, 3, 7)<<endl;
    return 0;
}
You can also try this code with Online C++ Compiler
Run Code

 

The above code will find the largest number among three numbers using the max() function in c++. The output of the above code is the same as method 1.

Output

Time Complexity: O(1) because the max() function has constant time complexity.

Space Complexity: O(1) because only one extra variable is used.

Try and compile with online c++ compiler.

Also Read - Strong number in c

FAQs

  1. Which is the best method to find the largest number among three numbers in C++?
    Both the methods mentioned above are best for finding the largest number in c++ as they have a constant time and space complexity.
     
  2. What are the disadvantages of using the comparison operator method?
    The main disadvantage of using the comparison operator method is that this method may become lengthy if we need to compare more than three numbers.
     
  3. How to find the smallest number among the three numbers?
    We may use the same method with a few modifications for finding the smallest number among the three numbers. The changes are as follows:-
    a. Less than(<) operator can be used instead of greater than(>) operator in method 1 
    b. min() function should be used instead of max() function in method 2.

Key Takeaways

In this article, we have extensively discussed how to find the largest number among three numbers in C++ using different methods.

We hope that this blog has helped you enhance your knowledge on how to find the largest number among three numbers in C++, and if you would like to learn more, check out our articles on STL containers in C++Data Types in C++, and Implementing Sets Without C++ STL Containers. Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass