×

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 )

Related Topics

C Program of Fencing the Ground

The college's ground is rectangular. Fencing the ground the management makes the decision to construct a fence around the ground. They planned to wrap a thick rope around the ground...

1 minute read.

Control statement in C

What is a control statement? A control statement helps us to control the flow of the program. The control statement helps us to execute the program's instructions in a user-defined order....

8 minutes read.

Hook() function in C

Use of hook () function in C Introduction:  The hook () is a function used in the C programming language. The basic concept of the hook() function is that it can remove the function...

3 minutes read.

Flow chart of While loop in C

This is a flowchart that represents the process of executing the while loop in the C programming language.Generally, as we know there are three main components of while loop:1. The...

3 minutes read.

Advantages of Dynamic Memory Allocation in C

Dynamic Memory Allocation Dynamic memory allocation is a process of assigning or allocating memory to a variable during the execution of a program. Dynamic memory allocation provides storage according to the...

3 minutes read.

GPA Calculator in C

GPA stands for Grade Point Average. This GPA is used to measure the student's academic performance in educational institutes. By using this, segregation takes place and lets us know how...

4 minutes read.

What is 2s Complement in C?

The complement of 2s in C is generated from the 1s in C. As we know, the 1s complement of a binary number is generated by converting bit 1 to...

4 minutes read.

Flow Chart of For loop in C

This is a flowchart that represents the process of executing the for loop in the C programming language. Generally, as we know there are three main components of for loop:1. The...

3 minutes read.

Storage class in C

Storage class in C defines the scope, the visibility, and the lifetime of variables and functions. In other words, storage classes are used to describe the features of variables and...

3 minutes read.

Character Class Tests in C

Introduction The <ctype.h> is a header file in C which is used to declare functions for testing characters. This header file of the C standard library is used to declare multiple...

4 minutes read.

Flexible Array Members in a Structure in C

There is flexibility to declare array from c99 generation onwards that declaration of the collections can be made possible without even mentioning its dimension, which means that the displays are...

4 minutes read.

Write() function in c

As the name suggests the write () function is used to write the file descriptor. In other words it is used to write any file name without specifying file name,...

3 minutes read.

Builtin functions of GCC compiler

Certain built-in functions are present in the GCC compiler. These features are as follows: Function_ built in_popcount (x) The number of 1s in data of the integer type can be counted using...

3 minutes read.

Binomial Coefficient Program in C

What is Binomial coefficient? In the given set of n possibilities, the binomial coefficient(n,k) indicates the order of choosing 'K' results from those possibilities. Binomial coeeficient of posistive n and k...

3 minutes read.

Classification of Programming language

 Classification of Programming language  The programming language represents a set of instructions compiled along with each other to perform a specific task provided by the CPU (Central Processing Unit). A programming...

3 minutes read.

Git Hooks

Git Hooks Overview Just like other version control systems, Git has its way to deliver customized scripts whenever some important activity occurs. The hooks act just like the triggers or catalysts...

4 minutes read.

C Switch Statements

Switch-case statement comes under the Selection control statement; there are four types of Control statements Decision-making statements (if, if-else)Selection statements (Switch-case)Iteration statements (for, while, do-while)Jump statements (break, continue, goto) If we want...

4 minutes read.

Pointers in C

Pointers in C: In the C programming language, a pointer is a pointer variable that points to the address of the other variable. It also stores the address of the...

4 minutes read.

Compilation Process in C language

What is the Compilation Process?  The compilation is a method whereby the source code is converted into object code. It is achieved with compiler assistance. The compiler tests the source code for syntactic...

3 minutes read.

Recursion in C

Recursion in C: In the C programming language, the concept known as recursion exists that is a technique in which a function calls itself either directly or indirectly. It allows...

4 minutes read.