C++ Program to Find Largest Among Three Numbers
Last Updated :
14 Oct, 2024
Given three numbers, the task is to find the largest number among them in C++.
Examples
Input: a = 1, b = 2, c = 11
Output: 11
Explanation: Since 11 is the largest number among all numbers.
Input: a = 9, b = 7, c = 5
Output: 9
Explanation: Since 9 is the largest number among all numbers.
Find Largest Among Three Numbers in C++
The simplest approach is to use the if-else-if ladder to find the largest of three numbers. First, we find the larger number among two of the numbers. Then compare this larger number to the third number to find the largest.
Approach
Consider a, b and c to be the three numbers:
- First, check if a is greater than both b and c. If yes, then a is the largest.
- If a is not the largest, check if b is greater than or equal to c. If yes, then b is the largest.
- If neither of these conditions is true, then c is the largest.
Code Implementation
C++
// C++ Program to find largest among three
// numbers using if-else-if ladder
#include <iostream>
using namespace std;
int main() {
int a = 1, b = 2, c = 11;
// Finding the largest by comparing using
// relational operators with if-else
if (a >= b) {
if (a >= c)
cout << a;
else
cout << c;
}
else {
if (b >= c)
cout << b;
else
cout << c;
}
return 0;
}
We can shorten this code by grouping multiple conditions in a single if statement using logical AND operator. (&&)
C++
// C++ program to find the maximum number among three
// numbers using compound expression in if-else
#include <iostream>
using namespace std;
int main() {
int a = 1, b = 2, c = 11;
// Finding largest using compound expressions
if (a >= b && a >= c)
cout << a;
else if (b >= a && b >= c)
cout << b;
else
cout << c;
return 0;
}
Other Methods to Find Largest Among Three Numbers
Apart from if-else statement, there are also the other methods by which we can find the largest among three numbers in C++.
Using Inbuilt Function
In C++, the std::max() function can be used to find the largest value among the given set of values. Traditionally, it could only find the maximum of two numbers at once, however, starting from C++11, std::max() can also accept a set of values as initializer list (i.e. values inside braces {} ).
C++
// C++ Program to find the greatest of three
// numbers using in-built max()
#include <bits/stdc++.h>
using namespace std;
int main() {
int a = 1, b = 2, c = 11;
// Find largest number among three numbers using
// std::max() function
cout << max({a, b, c});
return 0;
}
Using Temporary Variable
We can also find the largest among three number using temporary variable. First, assume that one of the numbers is the largest and store it in a temporary variable. Then compare this temporary variable with remaining numbers and update it if the compared number is bigger. By the end, we will be left with the largest number in the temporary variable.
This method is the standard method for finding the maximum among the set of numbers if a built-in method is not available.
Code Implementation
C++
// C++ Program to find largest among three
// numbers using temporary variable
#include <bits/stdc++.h>
using namespace std;
int main() {
int a = 1, b = 2, c = 11;
// Assuming the maximum variable is a
int temp = a;
// Check if b is larger than temp or not
if (b > temp)
temp = b;
// Check if c is larger than temp or not
if (c > temp)
temp = c;
cout << temp;
return 0;
}
Similar Reads
C++ Program to Find the Second Largest Element in an Array In C++, an array is a data structure that is used to store multiple values of similar data types in a contiguous memory location. In this article, we will learn how to find the second largest element in an array in C++. Examples: Input: arr[] = {34, 5, 16, 14, 56, 7, 56} Output: 34 Explanation: The
3 min read
C++ Program to Find Largest Element in an Array In this article, we will learn to write a C++ program to find the largest element in the given array arr of size N. The element that is greater than all other elements is the largest element in the array. Recommended PracticeHelp a Thief!!!Try It! One of the most simplest and basic approaches to fin
2 min read
How to Find Largest Number in an Array in C++? In C++, arrays are used to store the collection of similar elements to be stored in adjacent memory locations. They can store data of any type such as int, char, float, etc. In this article, we will learn how to find the largest number in an array in C++. For Example,Input: myVector = {1, 3, 10, 7,
2 min read
C++ Program for Largest K digit number divisible by X Integers X and K are given. The task is to find highest K-digit number divisible by X. Examples: Input : X = 30, K = 3 Output : 990 990 is the largest three digit number divisible by 30. Input : X = 7, K = 2 Output : 98 An efficient solution is to use below formula. ans = MAX - (MAX % X) where MAX i
1 min read
Largest and smallest digit of a number Given a number N. The task is to find the largest and the smallest digit of the number.Examples : Input : N = 2346 Output : 6 2 6 is the largest digit and 2 is smallestInput : N = 5 Output : 5 5 Approach: An efficient approach is to find all digits in the given number and find the largest and the sm
7 min read