16. Write a program using ternary operator to check whether 27 is a multiple of 3 or not.
Answers
Program to Find the Largest Number using Ternary Operator
Last Updated: 03-10-2018
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>
int main()
{
// variable declaration
int n1 = 5, n2 = 10, max;
// Largest among n1 and n2
max = (n1 > n2) ? n1 : n2;
// Print the largest number
printf("Largest number between %d and %d is %d. ",
n1, n2, max);
return 0;