C Program To Find The Largest Number Using Conditional Operator or Ternary Operator

In this article, we are going to write a c program to find the Largest Number Using Ternary or Conditional Operator

In this article we will make the following program -:

  • C Program To Find The Largest of Two Numbers Using Conditional or Ternary Operator
  • C Program To Find The Largest of Three Numbers Using Conditional or Ternary Operator
  • C Program To Find The Largest of Four Numbers Using Conditional or Ternary Operator
  • C Program To Find The Largest of Five Numbers Using Conditional or Ternary Operator

To make and understand these programs well, first of all you need to know about Ternary operator. You can read about the Ternary operator in detail by clicking on the link given below.

Now without wasting time let’s start programming.

C Program To Find The Largest Number Using Conditional Operator or Ternary Operator
C Program To Find The Largest Number Using Ternary Operator

C Program To Find The Largest of Two Numbers Using Ternary Operator

Algorithm

  1. Program Start
  2. Variable Declaration
  3. Input two number
  4. Check the condition, ternary (conditional) operator.
  5. Display answer according the condition
  6. Program End

Flowchart

C Program To Find The Largest Number Using Ternary Operator

Program

//C program to find largest among two numbers using ternary operator

#include<stdio.h>

void main()
{ 
  // Variable declaration
   int a,b,larg;    

   printf("Enter two number\n");
   scanf("%d %d",&a,&b);

 // Largest among a and b
   larg = a>b?a:b; 
     
 //Display largest number
   printf("Largest Number is : %d",larg); 

}

Output

Enter two number
20

18
Largest Number is : 20

C Program To Find The Largest of Three Numbers Using Ternary Operator

Program

//C program to find largest among three numbers using ternary operator

#include<stdio.h>

void main()
{ 
  // Variable declaration
   int a,b,c,larg;    

   printf("Enter three number\n");
   scanf("%d %d %d",&a,&b, &c);

 // Largest among a, b and c
   larg = a>b?a>c?a:c:b>c?b:c; 
     
 //Display largest number
   printf("Largest Number is : %d",larg); 

}

Output

Enter Three Numbers
12
10
11

Largest Number is : 12

C Program To Find The Largest of Four Numbers Using Ternary Operator

Program

//C program to find largest among four numbers using ternary operator

#include<stdio.h>

void main()
{ 
  // Variable declaration
   int a,b,c,d,larg;    

   printf("Enter four number\n");
   scanf("%d %d %d %d",&a,&b, &c, &d);

 // Largest among a, b, c and d
   larg = ( (a>b && a>c && a>d) ? a : (b>c && b>d) ? b : (c>d)? c : d );
     
 //Display largest number
   printf("Largest Number is : %d",larg); 

}

Output

Enter Four Numbers
10
30
20
40

Largest Number is : 40

C Program To Find The Largest of Five Numbers Using Ternary Operator

Program

//C program to find largest among five numbers using ternary operator

#include<stdio.h>

void main()
{
  // Variable declaration
   int a,b,c,d,e,larg;

   printf("Enter five number\n");
   scanf("%d %d %d %d %d",&a,&b, &c, &d, &e);

 // Largest among a, b, c and d
   larg = ( (a>b && a>c && a>d && a>e) ? a : (b>c && b>d && b>e) ? b : (c>d && c>e)? c : (d>e)? d : e );

 //Display largest number
   printf("Largest Number is : %d",larg);

}

Output

Enter Five Numer
10
20
32
28
18

Largest Number is : 32

Conclusion

In this article, we have created all the following programs using Ternary operator

  • C Program To Find The Largest of Two Numbers Using Ternary Operator
  • C Program To Find The Largest of Three Numbers Using Ternary Operator
  • C Program To Find The Largest of Four Numbers Using Ternary Operator
  • C Program To Find The Largest of Five Numbers Using Ternary Operator

If you have difficulty to understand any program or have any doubts or questions, then tell me in the comment below.

Jeetu Sahu is A Web Developer | Computer Engineer | Passionate about Coding, Competitive Programming, and Blogging

Leave a Comment