Find elements

Java program to Find smallest of three numbers using ternary operator

Java program to Find smallest of three numbers using ternary operator

In this program, we will discuss a simple concept of the Java 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 Java 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 Java language

Program 1

//Java program to find smallest of three numbers
import java.util.Scanner;
class Small_Three_Ternary{
public static void main (String args[]){
Scanner scan=new Scanner(System.in);
//scanner object for user input
System.out.print("Enter three number: ");
//ask input from the user
int num1=scan.nextInt();
//Readinginput from user for num1
int num2=scan.nextInt();
//Reaing input from user for num2
int num3=scan.nextInt();
//Reading input from user for num3
int result=num3<(num1<num2?num1:num2)?num3:((num1<num2)? num1:num2);
//Find smallest number
System.out.println("Smallest number is: "+result);

}
}

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

Enter three numbers: 24
25
58
Smallest number is: 24

 

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 Java language

Program 2

//Java program to find smallest of three numbers using ternary
import java.util.Scanner;
class Small_Three_Ternary1{
public static void main (String args[]){
Scanner scan=new Scanner(System.in);
//scanner object for user input
System.out.print("Enter three number: ");
//Ask input from user
int num1=scan.nextInt();
//Readinginput from user for num1
int num2=scan.nextInt();
//Reaing input from user for num2
int num3=scan.nextInt();
//Reading input from user for num3
int temp=((num1<num2)? num1:num2);

int result=num3<temp?num3:temp;
System.out.println("Smallest number is: "+result);

}
}

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

Enter three numbers: 417
893
174
Smallest number is: 174

 

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

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

C program to Find smallest of three numbers using ternary operator
Program to divide two numbers without using '/ ' operator in Java
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