Skip to main content

C Program for smallest, largest of three number using ternary operator

Below running code will accept 3 Numbers from user.

Based on results given in expression in ternary operator program will return smallest number

// Program Name: C Program to find smallest number using Ternary operator
#include <stdio.h>

void main()
{
    int num1,num2,num3;
    int counter;
    printf("\n\tEnter first Integer Number\t");
    scanf("%d",&num1);
    printf("\n\tEnter Second Integer Number\t");
    scanf("%d",&num2);
    printf("\n\tEnter Third Integer Number\t");
    scanf("%d",&num3);
    
    
    // Ternary operator
    counter = (num1 < num2) ? (num1 < num3 ? num1:num3) : (num2 < num3 ? num2:num3);
    
    printf("\n\t Minimum of 3 Number is %d", counter);
}

Output


        Enter first Integer Number      11

        Enter Second Integer Number     90

        Enter Third Integer Number      32

         Minimum of 3 Number is 11

To find another method Click here . Program to find smallest number without comparison

Program to find largest or greatest of 3 number using ternary operator

Accept 3 numbers from user using scanf. User ternary operator to compare them and print result using printf. Here is complete code.

// Program Name: C Program to find largest number using Ternary operator
#include <stdio.h>

void main()
{
    int num1,num2,num3;
    int counter;
    printf("\n\tEnter first Integer Number\t");
    scanf("%d",&num1);
    printf("\n\tEnter Second Integer Number\t");
    scanf("%d",&num2);
    printf("\n\tEnter Third Integer Number\t");
    scanf("%d",&num3);
    
    
    // Ternary operator
    counter = (num1 > num2) ? (num1 > num3 ? num1:num3) : (num2 > num3 ? num2:num3);
    
    printf("\n\t Largest of 3 Number is %d", counter);
}

Output

Enter first Integer Number	4
Enter Second Integer Number	5
Enter Third Integer Number	6
Largest of 3 Number is 6