Java program to find the largest and smallest number

Java program to find the largest and smallest number

Write a Java program to find the largest and smallest number is one of the commonly asked Java coding interview questions. The interviewer may ask you to find the largest and smallest number among 3 numbers or from a set of N numbers. To add to it, the interviewer may also impose some restrictions like not to use loop or not to use an array or not to use any comparison operator. It may be difficult to arrive at a solution on the spot, so knowing the logic before going to the interview can be highly helpful. In this post we will see some of the possible solutions to find maximum and minimum among N numbers.

Java program to find the largest and smallest of 3 numbers without using comparison operator

In this program we will use some in-built Mathematical functions in Java to find the largest and smallest value.

Here is a sample output of the above program.

Java program to find the largest and smallest of 3 numbers using comparison operator

Sample output of running the above program.

Java Program to find largest and smallest between 3 numbers using Ternary Operator

The previous program can be written using ternary operator as shown below.

Output:

Java program to find Largest and Smallest Number in an Array

This program is used to find largest and smallest number from an array of N numbers.

Sample output:

Java program to find Largest and Smallest number from a list of integers

This program is used to find largest and smallest number from a list of numbers.

Here is the output of running the above program.

That’s all about the program to find largest and smallest number in Java. If you know any other method to solve the problem, post it in the comments section.

The following two tabs change content below.
Working as a Java developer since 2010. Passionate about programming in Java. I am a part time blogger.

Add Comment

Required fields are marked *. Your email address will not be published.