×

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

Call by Value and Call by Reference in C

Call by reference and call by value are two different ways of passing arguments to a function in C programming language. Call by reference means that the called function is...

4 minutes read.

Strcpy() in C

In C language many operations can be performed on a string. Characters in a sequence are known as string. Note:   To use this function we must include the #include<string.h> header file...

4 minutes read.

Types of Function in C

A function is a piece of code that accomplish a certain operation for a specific task. There are two types of functions in C: System-defined function (Library Function)User-defined function 1. System defined Function...

8 minutes read.

Fahrenheit to Celsius in C

Before going into conversion, we have to know what Fahrenheit and Celsius mean, these are both units to measure the temperature.In our daily life, we use both Fahrenheit and Celsius,...

1 minute read.

Exit control loop in C

To examine the termination condition for an exit, we usually use an exit control loop in C. If the evaluation for termination condition results in true, then the control would...

3 minutes read.

Assignment Operator Program in C

An assignment operator is a symbol used to assign a value to a variable in a programming language. The assignment operator is often the equal symbol (=) in programming languages....

12 minutes read.

Remove an element from an array in C

A collection of objects or pieces of the same data type stored in a single memory block is known as an array. A data structure called an array is used...

3 minutes read.

C Language Environment Setup

To compile C program, we must have GCC compiler installed on our machine. In this C tutorial, all the examples are compiled and tested using GCC compiler. Although we can...

3 minutes read.

C Data type

Data Types in C with Examples Data types are used to define the type of data that a variable can store to perform a specific operation.ANSI C provides three types of...

2 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.

What is String Comparison in C

String comparison is the process of comparing two strings (sequences of characters) to determine if they are equal, or if one is greater or less than the other. The comparison...

4 minutes read.

Types Of Structures In C

We can normally store elements of the same datatype with the help of an array in C programming. We can store multiple numbers of elements of a character data type...

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.

7 Best IDEs for C/C++ Developers in 2024

As we all know that in programming languages, C is known as the building block which cannot be contradicted, and C++ is the prolong kind of C and it can...

4 minutes read.

Difference between C and Java

C programming and Java programming are two of the earliest programming languages. C programming follows a procedural approach whereas Java programming follows an object-oriented approach. Java programming is a part...

3 minutes read.

Difference between Array and List in C

C# Array vs List is where the abstraction and implementation of human computing meet.  Arrays are incredibly related to the hardware concept of contiguous and contiguous memory, where each part is...

3 minutes read.

Merge and Merge sort with example in C

Assume we have two ordered Integer arrays, a[] and b[]. If we wish to combine them into another ordered array, such as c[], we can use the following straightforward technique. Compare...

3 minutes read.

Continue in C

C language: C language is a procedure oriented programming language. We can say that it is a platform dependent language. C language is introduced by Dennis Ritchie in the year 1970. We...

2 minutes read.

How to include graphics.h in C?

How to include graphics.h in code blocks? Graphics .h is a header that allows drawing lines, rectangles, ovals, arcs, polygons, pictures, and strings on a graphical window by giving access to...

6 minutes read.

Type qualifiers in C

Type qualifiers in C: In the C programming language, type qualifiers are the keywords that prepend to the variables to change their accessibility, i.e., we can tell that the type...

4 minutes read.