C++ Program - Find Largest Number among three Numbers
Three numbers x, y and z are given and the largest number among these three numbers can be found out using below methods:
Method 1: Using If statement
In the example below, only if conditional statements are used.
#include <iostream> using namespace std; static void largest(int, int, int); static void largest(int x, int y, int z) { int max = x; if (x >= y && x >= z) max = x; if (y >= x && y >= z) max = y; if (z >= x && z >= y) max = z; cout<<"largest number among "<<x<<", "<< y<<" and "<<z<<" is: "<<max<<"\n"; } int main() { largest(100, 50, 25); largest(50, 50, 25); return 0; }
The above code will give the following output:
largest number among 100 , 50 and 25 is: 100 largest number among 50 , 50 and 25 is: 50
Method 2: Using If-else statement
It can also be solved using If-else conditional statements.
#include <iostream> using namespace std; static void largest(int, int, int); static void largest(int x, int y, int z) { int max = x; if (x >= y && x >= z) max = x; else if (y >= x && y >= z) max = y; else max = z; cout<<"largest number among "<<x<<", "<< y<<" and "<<z<<" is: "<<max<<"\n"; } int main() { largest(100, 50, 25); largest(50, 50, 25); return 0; }
The above code will give the following output:
largest number among 100 , 50 and 25 is: 100 largest number among 50 , 50 and 25 is: 50
Method 3: Using Nested If-else statement
The above problem can also be solved using nested if-else conditional statements.
#include <iostream> using namespace std; static void largest(int, int, int); static void largest(int x, int y, int z) { int max = x; if (x >= y) { if(x >= z) max = x; else max = z; } else { if(y >= z) max = y; else max = z; } cout<<"largest number among "<<x<<", "<< y<<" and "<<z<<" is: "<<max<<"\n"; } int main() { largest(100, 50, 25); largest(50, 50, 25); return 0; }
The above code will give the following output:
largest number among 100 , 50 and 25 is: 100 largest number among 50 , 50 and 25 is: 50
Method 4: Using ternary operator
The ternary operator can also be used here.
#include <iostream> using namespace std; static void largest(int, int, int); static void largest(int x, int y, int z) { int max = x; max = (x > y)? ((x > z)? x : z) : ((y > z)? y : z); cout<<"largest number among "<<x<<", "<< y<<" and "<<z<<" is: "<<max<<"\n"; } int main() { largest(100, 50, 25); largest(50, 50, 25); return 0; }
The above code will give the following output:
largest number among 100 , 50 and 25 is: 100 largest number among 50 , 50 and 25 is: 50
Recommended Pages
- C++ Program - To Check Prime Number
- C++ Program - Bubble Sort
- C++ Program - Selection Sort
- C++ Program - Maximum Subarray Sum
- C++ Program - Reverse digits of a given Integer
- C++ - Swap two numbers
- C++ Program - Fibonacci Sequence
- C++ Program - Insertion Sort
- C++ Program - Find Factorial of a Number
- C++ Program - Find HCF of Two Numbers
- C++ Program - Merge Sort
- C++ Program - Shell Sort
- Stack in C++
- Queue in C++
- C++ Program - Find LCM of Two Numbers
- C++ Program - To Check Whether a Number is Palindrome or Not
- C++ Program - To Check Whether a String is Palindrome or Not
- C++ Program - Heap Sort
- C++ Program - Quick Sort
- C++ - Swap Two Numbers without using Temporary Variable