Find elements

Find Largest of three numbers using ternary operator in C++

Find Largest of three numbers using ternary operator in C++

In this program, we will discuss a simple concept of the find Largest of three numbers using ternary operator in C++

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

Largest of three numbers

Using ternary operator to to find the largest

Using ternary operator to find the largest in two line

In this program, we will discover the largest number out of three numbers using ternary operator in C++ language

Program 1

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{

    int num1,num2,num3; //variables declaration
    cout<<"Please Enter three numbers: ";
    //Ask input from the user
   cin>>num1>>num2>>num3;
    //Reading the input from user and store the variables num1 ,num2 and num3

int temp=(num1>num2)? num1:num2;
//Compare num1 and num2 using ternary operator
//Assign the result in the temp variable
int largest= num3>temp?num3:temp;
//Compare num3 and temp variable using ternary operator
//Assign the result in the largest variable
cout<<"Largest number is: "<<largest;
//Display result on the screen
getch();
    return 0;
}

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

Please Enter three numbers: 99
56
45
Largest number is: 99

 

Using ternary operator to find the largest in one line

In this program, we will discover the largest number out of three numbers using ternary operator in C++ language

Program 2

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{

    int n1,n2,n3; //variables declaration
    cout<<"Please Enter three numbers: ";
    //Ask input from the user
   cin>>n1>>n2>>n3;
    //Reading the input from user and store the variables

int result=n3>(n1>n2?n1:n2)?n3:((n1>n2)? n1:n2);
//find largest using ternary operator and assign the result to result variable
cout<<"Largest number is: "<<result;
//Display result on the screen
getch();
    return 0;
}

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

Please Enter three numbers: 612
819
578
Largest number is: 819

 

In the above, all programs, Three variables num1,num2,num3 are compared one by one using ternary operator to find largest 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

Multiplication of two floating point numbers in C
Program to find largest of three numbers using ternary operator in C
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…

6 months ago

PHP Full Pyramid pattern program

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

6 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…

6 months ago

Python Full Pyramid star pattern program

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

9 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…

1 year 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,…

1 year ago