C++ Program to find the Greatest of three numbers
Program to find the Greatest of Three Numbers in C++
Here we will discuss how to find the greatest of three numbers in C++ entered by the user in c++ programming language.
To find the greatest of three numbers if else-if statement is used along with ‘>’ ad ‘&&’ operator in the condition.
Methods discussed in the post –
- Method 1: Using simple if-else conditions
- Method 2: Using ternary operators
- Method 3: Using inbuilt max functions
Method 1
Algorithm
For user inputs of numbers as first, second and third.
- Step 1: Check if first number is greater than second and third
- Print first is the greatest
- Step 2: Check if second number is greater than first and third
- Print second is the greatest
- Step 3: Third number has to be greatest
- Print third is the greatest
C++ Code
Run
#include <iostream> using namespace std; int main () { int first, second, third; first=10,second=20,third=30; //comparing first with other numbers if ((first >= second) && (first >= third)) cout << first << " is the greatest "; //comparing Second with other numbers else if ((second >= first) && (second >= third)) cout << second << " is the greatest"; else cout << third << " is the greatest"; return 0; } // Time complexity : O(1) // Space complexity : O(1)
Output
30 is the greatest
Method 2
Algorithm
For user inputs of numbers as first, second and third.
This method requires you know how ternary operator in C++ works
- Step 1: Store the largest between first and second using ternary operator in variable temp
- temp = first > second ? first:second;
- Step 2: Store the largest between temp and third using ternary operator in variable result
- result = temp > third ? temp:third;
- Step 3: Print value of result
C++ Code
Run
#include <iostream> using namespace std; int main () { int first, second, third; first=10,second=20,third=30; int temp, result; // find the largest bw first and second and store in temp temp = first > second ? first:second; // find the largest bw temp and third and finally printing it result = temp > third ? temp:third; // the above two ternary statements can be condensed into one ternary statement //result = third > (first > second ? first : second) ? third : ((first > second) ? first : second); cout << result << " is the largest"; return 0; } // Time complexity : O(1) // Space complexity : O(1)
Output
30 is the largest
Method 3
Algorithm
For user inputs of numbers as first, second and third.
This method uses internal inbuilt function max(a, b) which returns the larger value.
C++ Code
#include <iostream> #include <algorithm> using namespace std; int main () { int first, second, third; first=10,second=20,third=30; int result; result = max(first,max(second, third)); cout << result << " is the largest"; return 0; } // Time complexity : O(1) // Space complexity : O(1)
Output
30 is the largest
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
- Positive or Negative number: C | C++ | Java | Python
- Even or Odd number: C | C++ | Java | Python
- Sum of First N Natural numbers: C | C++ | Java | Python
- Sum of N natural numbers: C | C++ | Java | Python
- Sum of numbers in a given range: C | C++ | Java | Python
- Greatest of two numbers: C | C++ | Java | Python
- Greatest of the Three numbers: C | C++ | Java | Python
- Leap year or not: C | C++ | Java | Python
- Prime number: C | C++ | Java | Python
- Prime number within a given range: C | C++ | Java | Python
- Sum of digits of a number: C | C++ | Java | Python
- Reverse of a number : C | C++ | Java | Python
- Palindrome number: C | C++ | Java | Python
- Armstrong number : C | C++ | Java | Python
- Armstrong number in a given range : C | C++ | Java | Python
- Fibonacci Series upto nth term : C | C++ | Java | Python
- Find the Nth Term of the Fibonacci Series : C | C++ | Java | Python
- Factorial of a number : C | C++ | Java | Python
- Power of a number : C | C++ | Java | Python
- Factor of a number : C | C++ | Java | Python
- Strong number : C | C++ | Java | Python
- Perfect number : C | C++ | Java | Python
- Automorphic number : C | C++ | Java | Python
- Harshad number : C | C++ | Java | Python
- Abundant number : C| C++ | Java | Python
- Friendly pair : C | C++ | Java | Python
Login/Signup to comment