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.
- Program to find the largest number among two numbers using ternary operator.
C
// C program to find largest among two// numbers using ternary operator#include <stdio.h>intmain(){// variable declarationintn1 = 5, n2 = 10, max;// Largest among n1 and n2max = (n1 > n2) ? n1 : n2;// Print the largest numberprintf("Largest number between %d and %d is %d. ",n1, n2, max);return0;}chevron_rightfilter_noneJava
// Java program to find largest// among two numbers using// ternary operatorclassGFG{publicstaticvoidmain(String args[]){// variable declarationintn1 =5, n2 =10, max;// Largest among n1 and n2max = (n1 > n2) ? n1 : n2;// Print the largest numberSystem.out.println("Largest number between "+ n1 +" and "+ n2 +" is "+ max +". ");}}// This code is contributed by Arnab Kunduchevron_rightfilter_noneC#
// C# program to find largest// among two numbers using// ternary operator using System;usingSystem;classGFG{publicstaticvoidMain(){// variable declarationintn1 = 5, n2 = 10, max;// Largest among n1 and n2max = (n1 > n2) ? n1 : n2;// Print the largest numberConsole.WriteLine("Largest number between "+n1 +" and "+ n2 +" is "+max +". ");}}// This code is contributed// by Subhadeepchevron_rightfilter_nonePython3
# Python program to find largest# between two numbers using# Ternary Operator.# As python doesn't have ternary operator,# we use conditional expression,# form of conditional expression:# exp1 if condition else exp2# if condition is true then exp1# is evaluated otherwise exp2# max is library function to find# the maximum number, so can't use# max as variablen1=5n2=10mx=n1ifn1 > n2elsen2# we also can use# mx=(n1>n2) and n1 or n2# str() function is used to# convert integer to string,# it is required as we are# concatenating integers with stringprint("Largest number between "+str(n1)+" and "+str(n2)+" is "+str(mx))# This code is contributed by Santanuchevron_rightfilter_nonePHP
<?php// PHP program to find largest// among two numbers using// ternary operator// variable declaration$n1= 5;$n2= 10;// Largest among n1 and n2$max= ($n1>$n2) ?$n1:$n2;// Print the largest numberecho"Largest number between ".$n1." and ".$n2." is ".$max;// This code is contributed// by Akanksha Rai(Abby_akku)chevron_rightfilter_noneOutput:Largest number between 5 and 10 is 10.
- Program to find the largest number among three numbers using ternary operator.
C
// C program to find largest among three// numbers using ternary operator#include <stdio.h>intmain(){// variable declarationintn1 = 5, n2 = 10, n3 = 15, max;// Largest among n1, n2 and n3max = (n1 > n2) ? (n1 > n3 ? n1 : n3) : (n2 > n3 ? n2 : n3);// Print the largest numberprintf("Largest number among %d, %d and %d is %d.",n1, n2, n3, max);return0;}chevron_rightfilter_noneJava
// Java program to find largest// among three numbers using// ternary operatorclassGFG{publicstaticvoidmain(String args[]){// variable declarationintn1 =5, n2 =10, n3 =15, max;// Largest among n1, n2 and n3max = (n1 > n2) ?(n1 > n3 ? n1 : n3) :(n2 > n3 ? n2 : n3);// Print the largest numberSystem.out.println("Largest number among "+ n1 +", "+ n2 +" and "+ n3 +" is "+ max +". ");}}// This code is contributed// by Arnab Kunduchevron_rightfilter_noneC#
// C# program to find largest// number among three numbers// using ternary operatorusingSystem;classGFG{publicstaticvoidMain(){// variable declarationintn1 = 5, n2 = 10, n3 = 15, max;// Largest among n1 and n2max = (n1 > n2 && n1 > n2) ?n1 : (n2 > n3) ? n2 : n3;// Print the largest numberConsole.WriteLine("Largest number among "+n1 +", "+ n2 +" and "+n3 +" is "+ max);}}// This code is contributed by Santanuchevron_rightfilter_nonePython3
# Python program to find largest# between two numbers using# Ternary Operator.# As python doesn't have ternary operator,# we use conditional expression,# form of conditional expression:# exp1 if condition else exp2# if condition is true then exp1# is evaluated otherwise exp2# max is library function to find# the maximum number, so can't use# max as variablen1=5n2=10mx=n1ifn1 > n2elsen2# we also can use# mx=(n1>n2) and n1 or n2# str() function is used to# convert integer to string,# it is required as we are# concatenating integers with stringprint("Largest number between "+str(n1)+" and "+str(n2)+" is "+str(mx))# This code is contributed by Santanuchevron_rightfilter_nonePHP
<?php// PHP program to find largest among three// numbers using ternary operator// variable declaration$n1= 5;$n2= 10;$n3= 15;// Largest among n1, n2 and n3$max= ($n1>$n2) ? ($n1>$n3?$n1:$n3) :($n2>$n3?$n2:$n3);// Print the largest numberecho"Largest number among ".$n1.", ".$n2." and ".$n3." is ".$max;?>chevron_rightfilter_noneOutput:Largest number among 5, 10 and 15 is 15.
- Program to find the largest number among four numbers using ternary operator.
C
// C program to find largest among four// numbers using ternary operator#include <stdio.h>intmain(){// variable declarationintn1 = 5, n2 = 10, n3 = 15, n4 = 20, max;// Largest among n1, n2, n3 and n4max = (n1 > n2 && n1 > n3 && n1 > n4) ?n1 :((n2 > n3 && n2 > n4) ?n2 :(n3 > n4 ? n3 : n4));// Print the largest numberprintf("Largest number among %d, %d, %d and %d is %d.",n1, n2, n3, n4, max);return0;}chevron_rightfilter_noneJava
// Java program to find largest// among four numbers using// ternary operatorclassGFG{publicstaticvoidmain(String args[]){// variable declarationintn1 =5, n2 =10,n3 =15, n4 =20, max;// Largest among n1, n2, n3 and n4max = (n1 > n2 && n1 > n3 && n1 > n4) ?n1 : ((n2 > n3 && n2 > n4) ?n2 : (n3 > n4 ? n3 : n4));// Print the largest numberSystem.out.println("Largest number among "+n1 +", "+ n2 +", "+ n3 +" and "+ n4 +" is "+ max +". ");}}// This code is contributed// by Arnab Kunduchevron_rightfilter_noneC#
// C# program to find largest// number among four numbers// using ternary operatorusingSystem;classGFG{publicstaticvoidMain(){// variable declarationintn1 = 5, n2 = 10,n3 = 15, n4 = 20, max;// Largest among n1 and n2max = (n1 > n2 && n1 > n2 && n1 > n2) ?n1 : (n2 > n3 && n2 > n4) ?n2 : (n3 > n4) ? n3 : n4;// Print the largest numberConsole.WriteLine("Largest number among "+n1 +", "+ n2 +", "+n3 +" and "+ n4 +" is "+ max);}}// This code is contributed by Santanuchevron_rightfilter_nonePython3
# Python program to find largest# among four numbers using# Ternary Operator.# As python doesn't have ternary operator,# we use conditional expression,# form of conditional expression:# exp1 if condition else exp2# if condition is true then exp1# is evaluated otherwise exp2# max is library function to find# the maximum number, so can't use# max as variablen1=5n2=10n3=15n4=20mx=(n1if(n1 > n2andn1 > n2andn1 > n4)else(n2if(n2 > n3andn3 > n4)else(n3ifn3 > n4elsen4)))# str() function is used to convert# integer to string, it is required# as we are concatenating integers# with stringprint("Largest number among "+str(n1)+", "+str(n2)+", "+str(n3)+" and "+str(n4)+" is "+str(mx))# This code is contributed by Santanuchevron_rightfilter_nonePHP
<?php// PHP program to find largest among four// numbers using ternary operator// variable declaration$n1= 5;$n2= 10;$n3= 15;$n4= 20;// Largest among n1, n2, n3 and n4$max= ($n1>$n2&&$n1>$n3&&$n1>$n4) ?$n1: (($n2>$n3&&$n2>$n4) ?$n2: ($n3>$n4?$n3:$n4));// Print the largest numberecho"Largest number among ".$n1.", ".$n2.", ".$n3." and ".$n4." is ".$max;?>chevron_rightfilter_noneOutput:Largest number among 5, 10, 15 and 20 is 20.
Recommended Posts:
- C++ | Nested Ternary Operator
- C/C++ Ternary Operator - Some Interesting Observations
- Implementing ternary operator without any conditional statement
- C program to Find the Largest Number Among Three Numbers
- C Program for Find largest prime factor of a number
- C Program to find largest element in an array
- C/C++ Program to Find the Number Occurring Odd Number of Times
- To find sum of two numbers without using any operator
- C Program for Find sum of odd factors of a number
- C Program to Find minimum sum of factors of number
- C/C++ Program to find Product of unique prime factors of a number
- C/C++ Program for Greedy Algorithm to find Minimum number of Coins
- C / C++ Program for Largest Sum Contiguous Subarray
- Output of C programs | Set 55 (Ternary Operators)
- C Program for Program to find area of a circle
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



