Find elements

C program to Find smallest of three numbers using ternary operator

C program to Find smallest of three numbers using ternary operator

In this program, we will discuss a simple concept of the C program to Find smallest of three numbers using ternary operator

In this topic, we are going learn how to find the smallest number from given three numbers using ternary operator in C programming language.

Find smallest of three numbers

Using ternary operator to find the Smallest

Using ternary operator to find the smallest – method 1

In this program , we will find the smallest number from given three numbers using ternary operator in C programming  language

Program 1

//Find smallest of three using ternary
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int num1,num2,num3; //declare three variables
    printf("Enter three numbers: ");
    //Ask input from the user
    scanf("%d %d %d",&num1,&num2,&num3);
    //Reading the input from user for numbers
       int result=num3<(num1<num2?num1:num2)?num3:((num1<num2)? num1:num2);
       //find smallest number of three using ternary operator
        printf("\n The Smallest number is: %d ",result);
        //display result on the screen
        getch();
    return 0;
}

When the above code is executed, it produces the following result

Enter three numbers: 12
34
67
The smallest number is: 12

Using ternary operator to find the smallest –method 2

In this program , we will find the smallest number from given three numbers using ternary operator in C programming  language

Program 2

//Find smallest of three using ternary
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int num1,num2,num3; //declare three variables
    printf("Enter three numbers: ");
    //Ask input from the user
    scanf("%d %d %d",&num1,&num2,&num3);
    //Reading the input from user for numbers
      int temp=((num1<num2)? num1:num2);
int result=num3<temp?num3:temp;
       //find smallest number of three using ternary operator
        printf("\n The Smallest number is: %d ",result);
        //display result on the screen
        getch();
    return 0;
}

When the above code is executed, it produces the following result

Enter three numbers: 768
234
596
The smallest number is: 234

 

In the above, all programs, Three variables num1,num2,num3 are compared one by one using ternary operator to find smallest one.

 

Suggested for you

If statements in C language

Nested if statements in C language

Operator in C language

Data type in C language

Variable in C language

 

Similar post

 largest number of three number in C

 largest number of three number in C++

 largest number of three number in Python

 largest number of three number in C using function

 largest number of three number in C++ using function

 largest number of three number in Python using function

 largest number of three number in Java using method

 

Smallest number of three numbers in Java language

Smallest number of three numbers in C language

Smallest number of three numbers in C++ language

Smallest number of three numbers in Python language

Smallest number of three numbers using method in Java language

Smallest number of three numbers using function in C language

Smallest number of three numbers using function in C++ language

Smallest number of three numbers using function  in Python language

C++ program to Find smallest of three numbers using ternary operator
Java program to Find smallest of three numbers using ternary operator
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

1 month ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

1 month ago