Write a C program to find minimum from 3 numbers using the conditional operator.

As given in the program definition, we have to find out the minimum number using conditional operator. We all know that, conditional operator is a ternary operator which means it requires 3 operands to work.

## Tutorial 📺

Watch tutorial and learn how to solve this problem. 😉

## Steps 🪜

### To find minimum from 3 numbers

1. Either accept 3 integers from the user and store the value in 3 variables n1, n2 and n3 or initialize these variables at the time of declaration.
2. Use following statement to find out the minimum number. n1 < n2 ? (n1 < n3 ? n1 : n3) : (n2 < n3 ? n2 : n3)
3. You can simply put the above statement in printf() and display the output along with proper message.

## Code 💻

Variables are initialized at the time of declaration and display the minimum among 3 numbers.

``````#include <stdio.h>
#include <conio.h>

void main()
{
int n1=10, n2=8, n3=6;
clrscr();
// op1 ? op2 : op3;
printf("%d is minimum.", (n1 < n2 ? (n1 < n3 ? n1 : n3) : (n2 < n3 ? n2 : n3)));
getch();
}``````

Accept 3 numbers from the user and display the minimum among 3 numbers.

``````#include <stdio.h>
#include <conio.h>

void main()
{
int n1, n2, n3;
clrscr();
scanf("%d", &n1);
scanf("%d", &n2);
scanf("%d", &n3);
// op1 ? op2 : op3;
printf("%d is minimum.", (n1 < n2 ? (n1 < n3 ? n1 : n3) : (n2 < n3 ? n2 : n3)));
getch();
}``````

## Example 😍

Let’s assume, the user enters 3 numbers as 10, 8 and 6 which are stored in variables n1, n2 and n3 respectively.

First of all, it will check if n1 < n2 (10 < 8) or not. However, 10 is greater than 8, which means it is false. So, it will execute the other part of the conditional operator. Next, it will check if n2 < n3 (8 < 6) or not. We all know that 8 is not smaller than 6, which means, again it is false. So, it will execute the false part. Finally, it will display a message “6 is minimum.” as the output.

Let us take another example where the user enters 3 numbers as 34, 29 and 44.

Initially, it will check if 34 < 29 or not. However, 34 is not lesser than 29, which means it is false, so it will execute the false part. Furthermore, it will check if 29 < 44, which is true, so it will execute the true part. Finally, it will display the output as “29 is minimum.

This is how we find out minimum among 3 numbers using conditional operator.