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 <iostream>
#include <conio.h>
using namespace std;
int main()
{
    int num1,num2,num3; //declare three variables
    cout<<"Enter three numbers for find smallest: ";
    //Ask input from the user
    cin>>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
        cout<<"The Smallest number is: "<<result;
        //display result on the screen
        getch();
    return 0;
}

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

Enter three numbers for find smallest: 713
469
976
The smallest number is: 469

 

 

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 <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int num1,num2,num3; //declare three variables
    cout<<"Enter three numbers: ";
    //Ask input from the user
    cin>>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
        cout<<"The Smallest number is: "<<result;
        //display result on the screen
        getch();
    return 0;
}

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

Enter three numbers: 1456
3425
7890
The smallest number is: 1456

 

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

 Biggest number of three number in C

 Biggest number of three number in C++

 Biggest number of three number in Python

 Biggest number of three number in C using function

 Biggest number of three number in C++ using function

 Biggest number of three number in Python using function

 Biggest 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

Java program to find largest of three numbers using ternary operator
C 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

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

2 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago