
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Program to find the Largest Number using Ternary Operator in C++
In this problem, we are given some numbers. Our task is to create a Program to Find the Largest Number using Ternary Operator in C++.
The elements can be −
- Two Numbers
- Three Numbers
- Four Numbers
Code Description − Here, we are given some numbers (two or three or four). We need to find the maximum element out of these numbers using a ternary operator.
Let’s take a few examples to understand the problem,
Two Numbers
Input − 4, 54
Output − 54
Three Numbers
Input − 14, 40, 26
Output − 40
Four Numbers
Input − 10, 54, 26, 62
Output − 62
Solution Approach
We will use ternary Operator, for two, three and four element for finding the maximum element of the four.
Implementing Ternary operator for
Two numbers (a, b),
a > b ? a : b
Three numbers (a, b, c),
(a>b) ? ((a>c) ? a : c) : ((b>c) ? b : c)
Four numbers (a, b, c, d),
(a>b && a>c && a>d) ? a : (b>c && b>d) ? b : (c>d)? c : d
Program to illustrate the working of our solution for two numbers −
Example
#include <iostream> using namespace std; int main() { int a = 4, b = 9; cout<<"The greater element of the two elements is "<<( (a > b) ? a :b ); return 0; }
Output
The greater element of the two elements is 9
Program to illustrate the working of our solution for three numbers −
Example
#include <iostream> using namespace std; int findMax(int a, int b, int c){ int maxVal = (a>b) ? ((a>c) ? a : c) : ((b>c) ? b : c); return maxVal; } int main() { int a = 4, b = 13, c = 7; cout<<"The greater element of the two elements is "<<findMax(a, b,c); return 0; }
Output
The greater element of the two elements is 13
Program to illustrate the working of our solution for four numbers −
Example
#include <iostream> using namespace std; int findMax(int a, int b, int c, int d){ int maxVal= ( (a>b && a>c && a>d) ? a : (b>c && b>d) ? b : (c>d)? c : d ); return maxVal; } int main() { int a = 4, b = 13, c = 7, d = 53; cout<<"The greater element of the two elements is "<<findMax(a, b, c, d); return 0; }
Output
The greater element of the two elements is 53
- Related Articles
- Java program to find largest of the three numbers using ternary operators
- Ternary Operator in Java
- Ternary Operator in Python?
- Ternary Operator in C#
- Changing ternary operator into non-ternary - JavaScript?
- Conditional ternary operator ( ?: ) in C++
- Ternary Operator in Dart Programming
- Python Program to Find the Second Largest Number in a List Using Bubble Sort
- C/C++ Ternary Operator
- Java Ternary Operator Examples
- Java program to find the largest number in an array
- Python program to find the largest number in a list
- How to overload python ternary operator?
- Java program to find smallest of the three numbers using ternary operators
- Difference between the Ternary operator and Null coalescing operator in php
