Java Program to Find Smallest of Three Numbers Using Ternary Operator24 Dec 2024 | 4 min read In this section, we will learn how to create a Java program to find the smallest of three numbers. Along with this, we will also learn how to find the smallest of three numbers in Java using the ternary operator. Using Ternary OperatorBefore moving to the program, let's understand the ternary operator. It is a part of the Java conditional statement. It contains three operands. It evaluates a Boolean expression and assigned a value based on the result. We can use it in place of the if-else statement. The syntax of the ternary operator is as follows: If the above expression returns true the value before the colon is assigned to the variable variable_name, else the value after the colon is assigned to the variable. Let's use the ternary operator in a Java program to compare three variables. In the following program, we have used two ternary operators to compare three numbers. SmallestNumberExample1.java Output: Enter the first number: 23 Enter the second number: 11 Enter the third number: 67 The smallest Number is: 11 We can also compare all the three numbers by using the ternary operator in a single statement. If we want to compare three numbers in a single statement, we should use the following statement: In the following program, we have used a single statement to find the smallest of three numbers. SmallestNumberExample2.java Output: Enter the first number: 45 Enter the second number: 87 Enter the third number: 34 The smallest number is: 34 Using if-else..ifSmallestNumberExample3.java Output: 8 is the smallest number Using nested-if StatementSmallestNumberExample4.java Output: The smallest number is: 169 Let's see another logic to find the smallest of three numbers. SmallestNumberExample5.java Output 1: Enter three integers: 12 110 9 The smallest number is: 9 Output 2: Enter three integers: 12 12 12 The numbers are same. Next TopicJava biconsumer interface |
In this program, we will create a singly linked list and add a new node at the beginning of the list. To accomplish this task, we will store head to a temporary node temp. Make newly added node as the new head of the list....
5 min read
Java Convert int to long We can convert int to long in java using assignment operator. There is nothing to do extra because lower type can be converted to higher type implicitly. It is also known as implicit type casting or type promotion. Java int to long Example Let's...
1 min read
Problem Statement The problem states that a gardener wants to water a garden (single-dimensional) by opening the minimum number of taps. The task is to find the minimum number of taps that should be open to water the whole garden, return -1 if the garden cannot be...
17 min read
Java Convert Timestamp to Date We can convert Timestamp to Date in java using constructor of java.util.Date class. The constructor of Date class receives long value as an argument. So, you need to convert Timestamp object into long value using getTime() method of java.sql.Timestamp class. Let's see the constructor...
2 min read
In this program, we need to separate each character from the string. CHARACTERS C H A R A C T E R S In computer science, collection of characters including spaces is called as string. To separate an individual character from the string, individual characters are accessed through...
1 min read
Program to print the elements of an array in reverse order In this program, we need to print the elements of the array in reverse order that is; the last element should be displayed first, followed by second last element and so on. Above array in reversed...
2 min read
In this section, we have covered different logics in Java programs to find GCD of two numbers. Greatest Common Divisor: It is the highest number that completely divides two or more numbers. It is abbreviated for GCD. It is also known as the Greatest Common Factor (GCF)...
4 min read
In this section, we will learn what is a strontio number and also create Java programs to check if the given number is strontio. The strontio number program frequently asked in Java coding tests and academics. Strontio Number Strontio numbers are those four digits numbers when multiplied by...
3 min read
Java String to long We can convert String to long in java using Long.parseLong() method. Scenario It is generally used if we have to perform mathematical operations on the string that contains long number. Whenever we get data from textfield or textarea, entered data is received as a string....
1 min read
In this program, we will create a doubly linked list and sort nodes of the list in ascending order. Original List: Sorted List: To accomplish this, we maintain two pointers: current and index. Initially, current point to head node and index will point to node to current....
7 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India