Find elements

Java program to find largest of three numbers using ternary operator

Java program to find largest of three numbers using ternary operator

In this program, we will discuss a simple concept of the Java program to find largest of three numbers using ternary operator

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

find largest of three numbers

Using ternary operator to find the largest

Using ternary operator to find the largest in one line

In this program , we will find the largest number from given three numbers using ternary operator in Java language

Program 1

import java.util.Scanner;
class Large_Three_Conditional{
public static void main (String args[]){
int result;
Scanner scan=new Scanner(System.in);
//scanner object for user input
System.out.print("Enter three number: ");
int num1=scan.nextInt();//Reading input from user for num1
int num2=scan.nextInt();//Reading input from user for num2
int num3=scan.nextInt();//Reading input from user for num3
result=num3>(num1>num2?num1:num2)?num3:((num1>num2)? num1:num2);
System.out.println("Largest number is: "+result);

}
}

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

Enter three numbers: 2467
9536
6574
Largest number is: 9536

 

Using ternary operator to find the largest in two line

In this program , we will find the largest number from given three numbers using ternary operator in Java language

Program 2

import java.util.Scanner;
class Large_Three_Conditiona2{
public static void main (String args[]){

Scanner scan=new Scanner(System.in);
System.out.print("Enter three numbers: ");
//Ask input from the user
int num1=scan.nextInt();//Reading input from the user for num1
int num2=scan.nextInt();//Reading input from the user for num2
int num3=scan.nextInt();//Reading input from the user for num3
int temp=(num1>num2)? num1:num2;//Compare num1 and num2 using ternary operator
//the result to assign the temp variable
int largest= num3>temp?num3:temp;//Compare num3 and temp
//the result to assign the largest variable
System.out.println("Largest number is: "+largest);
//Display result on the screen

}
}

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

Enter three numbers: 245
567
879
Largest number is: 879

 

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

Operator in Java language

If statements in Java language

Nested if statements in Java language

Data type in Java language

Method in Java 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

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

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

2 days ago

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…

2 months ago