C Program To Find Smallest of Two Numbers Using Conditional Operator
Algorithm
Program Start
Variable Declaration
Input two number
Check the condition, ternary (conditional) operator.
Display answer according the condition
Program End
Program -:
//C program to find Smallest among two numbers
//using Conditional operator or ternary operator
#include<stdio.h>
void main()
{
// Variable declaration
int a,b,small;
printf("Enter two number\n");
scanf("%d %d",&a,&b);
// Smallest among a and b
small = a<b?a:b;
//Display smallest number
printf("Smallest among 2 Number is : %d",small);
}
Output -:
Enter two number
12
17
Smallest among 2 Number is : 12
C Program To Find Smallest of Three Numbers Using Conditional Operator
Program -:
//C program to find Smallest among three numbers
//using Conditional operator or ternary operator
#include<stdio.h>
void main()
{
// Variable declaration
int a,b,c,small;
printf("Enter Three Number\n");
scanf("%d %d %d",&a,&b,&c);
// Small among a, b and c
small = a<b?a<c?a:c:b<c?b:c;
//Display smallest number
printf("Smallest Among 3 Number is : %d",small);
}
Output -:
Enter Three Number
12
10
17
Smallest Among 3 Number is : 10
C Program To Find Smallest of Four Numbers Using Conditional Operator
Program -:
//C program to find Smallest among four numbers using Conditional or ternary operator
#include<stdio.h>
void main()
{
// Variable declaration
int a,b,c,d,small;
printf("Enter four number\n");
scanf("%d %d %d %d",&a,&b, &c, &d);
// Smallest among a, b, c and d2
small = ( (a<b && a<c && a<d) ? a : (b<c && b<d) ? b : (c<d)? c : d );
//Display Smallest number
printf("Smallest Number is : %d",small);
}
Output -:
Enter Four Number
12
4
23
6
Smallest Number is : 4
C Program To Find Smallest of five Numbers Using Conditional Operator
Program -:
//C program to find Smallest among five numbers using ternary operator
#include<stdio.h>
void main()
{
// Variable declaration
int a,b,c,d,e,small;
printf("Enter five number\n");
scanf("%d %d %d %d %d",&a,&b, &c, &d, &e);
// Smallest among a, b, c and d
small = ( (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 Smallest number
printf("Smallest Number is : %d",small);
}
Output:
Enter five number
12
4
32
5
3
Smallest Numbers is : 3
C Program To Find Smallest of Two Numbers Using Conditional Operator
Algorithm
Program -:
Output -:
C Program To Find Smallest of Three Numbers Using Conditional Operator
Program -:
Output -:
C Program To Find Smallest of Four Numbers Using Conditional Operator
Program -:
Output -:
C Program To Find Smallest of five Numbers Using Conditional Operator
Program -:
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer