C Program to Find the Largest Number using Ternary Operator
In this tutorial, we will write some program to determine which of the provided integers is larger using the ternary operator, also known as the conditional operator in C.
Ternary Operator:
The ternary operator is an operator that can be found in various programming languages, as opposed to the usual one or two operands that most operators utilise. It offers a technique to compact an easy if-else block.
The goal is to create a programme that uses the ternary operator to identify the largest number among:
- Two Digits
- Three Digits
- Four Digits
Examples:
Input: 54, 79
Output:
The largest of the two numbers ( 54, 79 ) is: 79.
Input: 87, 75, 21
Output:
The largest of the three numbers ( 87, 75, 21 ) is: 87.
The form of a ternary operator is as follows:
Expression1 ? Expression2 : Expression3
Expression 1 will always be evaluated. Expression1's output determines how Expression2 and Expression3 are carried out. Expression2 will be assessed if the result of Expression1 is non-zero; else, Expression3 will be assessed.
1.Finding the largest number between two numbers using the ternary operator in a C program
Algorithm
- Start
- Variables’ declaration
- Put two numbers
- Check the ternary (conditional) operator's condition.
- Put on view the numbers asper condition
- Terminate
C Program:
// C program to find largest of two numbers using ternary operator
// Header Files
#include <stdio.h>
#include <conio.h>
// Main Function
int main()
{
// Declaring Variables
int num1, num2 , max_no;
// Asks user for two inputs
printf ( "Put any two digits\n" );
// Storing inputs in the variables
scanf( "%d %d", & num1 , & num2 );
// Largest between num1 and num2
max_no = ( num1 > num2 ) ? num1 : num2;
// Printing the largest number
printf ( "The largest of the two numbers ( %d, %d ) is: %d. ",
num1, num2, max_no );
return 0;
}
Output ( If the user puts 31 and 47 as two inputs ):
Put any two digits
31
47
The largest of the two numbers ( 31, 47 ) is: 47.
2. Finding the largest number among three numbers using the ternary operator in a C program
C Program:
// C program to find largest of three numbers using ternary operator
// Header Files
#include <stdio.h>
#include <conio.h>
// Main Function
int main()
{
// Declaring Variables
int num1, num2 , num3 , max_no;
// Asks user for three inputs
printf ( "Put any three digits\n" );
// Storing inputs in the variables
scanf( "%d %d %d", & num1 , & num2 , & num3 );
// Largest of num1 , num2 and num3
max_no = ( num1 > num2 ) ? ( num1 > num3 ? num1 : num3 ) : ( num2 > num3 ? num2 : num3 );
// Printing the largest number
printf ( "The largest of the two numbers ( %d, %d and %d ) is: %d. ",
num1, num2, num3, max_no );
return 0;
}
Output ( If the user puts 31, 59 and 12 as three inputs ):
Put any three digits
31
59
12
The largest of the two numbers ( 31, 59 and 12 ) is: 59.
3. Finding the largest number among four numbers using the ternary operator in a C program
C Program:
// C program to find largest of four numbers using ternary operator
// Header Files
#include <stdio.h>
#include <conio.h>
// Main Function
int main()
{
// Declaring Variables
int num1, num2 , num3 , num4 , max_no;
// Asks user for four inputs
printf ( "Put any four digits\n" );
// Storing inputs in the variables
scanf ( "%d %d %d %d", & num1 , & num2 , & num3 , & num4 );
// Largest of num1 , num2 , num3 and num4
max_no = ( num1 > num2 && num1 > num3 && num1 > num4 ) ?
num1 :
(( num2 > num3 && num2 > num4 ) ?
num2 :
( num3 > num4 ? num3 : num4 ));
// Printing the largest number
printf ( "The largest of the two numbers ( %d, %d , %d and %d ) is: %d. ",
num1, num2, num3, num4, max_no );
return 0;
}
Output ( If the user puts 21, 19, 42 and 35 as four inputs ):
Put any four digits
21
19
42
35
The largest of the two numbers ( 21, 19 , 42 and 35 ) is: 42.
- Time Complexity of the aforementioned programs:
O ( 1 )
- Space Complexity of the aforementioned programs:
O ( 1 )