The task is to write a program to find the largest number using ternary operator among:
- Two Numbers
- Three Numbers
- Four Numbers
Examples:
Input : 10, 20
Output :
Largest number between two numbers (10, 20) is: 20
Input : 25 75 55 15
Output :
Largest number among four numbers (25, 75, 55, 15) is: 75
A Ternary Operator has the following form,
exp1 ? exp2 : exp3
The expression exp1 will be evaluated always. Execution of exp2 and exp3 depends on the outcome of exp1. If the outcome of exp1 is non zero then exp2 will be evaluated, otherwise, exp3 will be evaluated.
C++
#include <iostream>
using namespace std;
int main()
{
int n1 = 5, n2 = 10, max;
max = (n1 > n2) ? n1 : n2;
cout << "Largest number between "
<< n1 << " and " << n2
<< " is " << max << "." ;
return 0;
}
|
C
#include <stdio.h>
int main()
{
int n1 = 5, n2 = 10, max;
max = (n1 > n2) ? n1 : n2;
printf ( "Largest number between %d and %d is %d. " ,
n1, n2, max);
return 0;
}
|
Java
class GFG
{
public static void main(String args[])
{
int n1 = 5 , n2 = 10 , max;
max = (n1 > n2) ? n1 : n2;
System.out.println( "Largest number between " + n1 +
" and " + n2 + " is " + max + ". " );
}
}
|
Python3
n1 = 5
n2 = 10
mx = n1 if n1 > n2 else n2
print ( "Largest number between " + str (n1) +
" and " + str (n2) +
" is " + str (mx))
|
C#
using System;
class GFG
{
public static void Main()
{
int n1 = 5, n2 = 10, max;
max = (n1 > n2) ? n1 : n2;
Console.WriteLine( "Largest number between " +
n1 + " and " + n2 + " is " +
max + ". " );
}
}
|
PHP
<?php
$n1 = 5; $n2 = 10;
$max = ( $n1 > $n2 ) ? $n1 : $n2 ;
echo "Largest number between " .
$n1 . " and " . $n2 . " is " . $max ;
|
Javascript
<script>
var n1 = 5, n2 = 10, max;
max = (n1 > n2) ? n1 : n2;
document.write( "Largest number between "
+ n1 + " and " + n2
+ " is " + max + "." );
</script>
|
Output:
Largest number between 5 and 10 is 10.
C++
#include <iostream>
using namespace std;
int main()
{
int n1 = 5, n2 = 10, n3 = 15, max;
max = (n1 > n2) ?
(n1 > n3 ? n1 : n3) :
(n2 > n3 ? n2 : n3);
cout << "Largest number among "
<< n1 << ", " << n2 << " and "
<< n3 << " is " << max << "." ;
return 0;
}
|
C
#include <stdio.h>
int main()
{
int n1 = 5, n2 = 10, n3 = 15, max;
max = (n1 > n2) ? (n1 > n3 ? n1 : n3) : (n2 > n3 ? n2 : n3);
printf ( "Largest number among %d, %d and %d is %d." ,
n1, n2, n3, max);
return 0;
}
|
Java
class GFG
{
public static void main(String args[])
{
int n1 = 5 , n2 = 10 , n3 = 15 , max;
max = (n1 > n2) ?
(n1 > n3 ? n1 : n3) :
(n2 > n3 ? n2 : n3);
System.out.println( "Largest number among " + n1 +
", " + n2 + " and " + n3 +
" is " + max + ". " );
}
}
|
Python3
n1 = 5
n2 = 10
n3 = 15
mx = (n1 if (n1 > n2 and n1 > n3) else
(n2 if (n2 > n1 and n2 > n3) else n3))
print ( "Largest number among " + str (n1) +
" , " + str (n2) +
" and " + str (n3) +
" is " + str (mx))
|
C#
using System;
class GFG
{
public static void Main()
{
int n1 = 5, n2 = 10, n3 = 15, max;
max = (n1 > n2 && n1 > n2) ?
n1 : (n2 > n3) ? n2 : n3;
Console.WriteLine( "Largest number among " +
n1 + ", " + n2 + " and " +
n3 + " is " + max);
}
}
|
PHP
<?php
$n1 = 5; $n2 = 10; $n3 = 15;
$max = ( $n1 > $n2 ) ? ( $n1 > $n3 ? $n1 : $n3 ) :
( $n2 > $n3 ? $n2 : $n3 );
echo "Largest number among " . $n1 . ", " . $n2 .
" and " . $n3 . " is " . $max ;
?>
|
Javascript
<script>
var n1 = 5, n2 = 10, n3 = 15, max;
max = (n1 > n2) ?
(n1 > n3 ? n1 : n3) :
(n2 > n3 ? n2 : n3);
document.write( "Largest number among "
+ n1 + ", " + n2 + " and "
+ n3 + " is " + max + "." );
</script>
|
Output:
Largest number among 5, 10 and 15 is 15.
C++
#include <iostream>
using namespace std;
int main()
{
int n1 = 5, n2 = 10, n3 = 15, n4 = 20, max;
max = (n1 > n2 && n1 > n3 && n1 > n4) ?
n1 : ((n2 > n3 && n2 > n4) ?
n2 : (n3 > n4 ? n3 : n4));
cout << "Largest number among "
<< n1 << ", " << n2 << ", "
<< n3 << " and " << n4
<< " is " << max << "." ;
return 0;
}
|
C
#include <stdio.h>
int main()
{
int n1 = 5, n2 = 10, n3 = 15, n4 = 20, max;
max = (n1 > n2 && n1 > n3 && n1 > n4) ?
n1 :
((n2 > n3 && n2 > n4) ?
n2 :
(n3 > n4 ? n3 : n4));
printf ( "Largest number among %d, %d, %d and %d is %d." ,
n1, n2, n3, n4, max);
return 0;
}
|
Java
class GFG
{
public static void main(String args[])
{
int n1 = 5 , n2 = 10 ,
n3 = 15 , n4 = 20 , max;
max = (n1 > n2 && n1 > n3 && n1 > n4) ?
n1 : ((n2 > n3 && n2 > n4) ?
n2 : (n3 > n4 ? n3 : n4));
System.out.println( "Largest number among " +
n1 + ", " + n2 + ", " + n3 +
" and " + n4 + " is " + max + ". " );
}
}
|
Python3
n1 = 5
n2 = 10
n3 = 15
n4 = 20
mx = (n1 if (n1 > n2 and n1 > n2 and n1 > n4)
else (n2 if (n2 > n3 and n3 > n4)
else (n3 if n3 > n4 else n4)))
print ( "Largest number among " + str (n1) + ", " +
str (n2) + ", " + str (n3) + " and " +
str (n4) + " is " + str (mx))
|
C#
using System;
class GFG
{
public static void Main()
{
int n1 = 5, n2 = 10,
n3 = 15, n4 = 20, max;
max = (n1 > n2 && n1 > n2 && n1 > n2) ?
n1 : (n2 > n3 && n2 > n4) ?
n2 : (n3 > n4) ? n3 : n4;
Console.WriteLine( "Largest number among " +
n1 + ", " + n2 + ", " +
n3 + " and " + n4 +
" is " + max);
}
}
|
PHP
<?php
$n1 = 5; $n2 = 10; $n3 = 15; $n4 = 20;
$max = ( $n1 > $n2 && $n1 > $n3 && $n1 > $n4 ) ?
$n1 : (( $n2 > $n3 && $n2 > $n4 ) ?
$n2 : ( $n3 > $n4 ? $n3 : $n4 ));
echo "Largest number among " . $n1 . ", " . $n2 . ", " .
$n3 . " and " . $n4 . " is " . $max ;
?>
|
Javascript
<script>
var n1 = 5, n2 = 10, n3 = 15, n4 = 20, max;
max = (n1 > n2 && n1 > n3 && n1 > n4)
? n1 : ((n2 > n3 && n2 > n4)
? n2 : (n3 > n4 ? n3 : n4));
document.write( "Largest number among " + n1
+ ", " + n2 + ", " + n3 + " and "
+ n4 + " is " + max + ". " );
</script>
|
Output:
Largest number among 5, 10, 15 and 20 is 20.
For all the above programs :
Time Complexity: O(1)
Auxiliary Space: O(1)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
24 Jun, 2022
Like Article
Save Article