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.
Contents
hide
C Program To Find The Largest of Two Numbers Using Ternary Operator
Algorithm
- Program Start
- Variable Declaration
- Input two number
- Check the condition, ternary (conditional) operator.
- Display answer according the condition
- Program End
Flowchart
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
Read More
- Download C Language Notes Pdf
- C Language Tutorial For Beginners
- C Programming Examples With Output
- 250+ C Programs for Practice PDF Free Download
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.