## 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.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import java.util.Scanner; public class MaxMinNumber { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("Enter first number: "); int n1 = in.nextInt(); System.out.println("Enter second number: "); int n2 = in.nextInt(); System.out.println("Enter third number: "); int n3 = in.nextInt(); int largest = Math.max(n1, Math.max(n2, n3)); System.out.println("Largest among (" + n1 + ", " + n2 + ", " + n3 + ") is: " + largest); int smallest = Math.min(n1, Math.min(n2, n3)); System.out.println("Smallest among (" + n1 + ", " + n2 + ", " + n3 + ") is: " + smallest); in.close(); } } |

Here is a sample output of the above program.

1 2 3 4 5 6 7 8 | Enter first number: 100 Enter second number: 13 Enter third number: 152 Largest among (100, 13, 152) is: 152 Smallest among (100, 13, 152) is: 13 |

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | import java.util.Scanner; public class MaxMinNumber { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("Enter first number: "); int n1 = in.nextInt(); System.out.println("Enter second number: "); int n2 = in.nextInt(); System.out.println("Enter third number: "); int n3 = in.nextInt(); int largest = n1; if (n2 > largest) { largest = n2; } if (n3 > largest) { largest = n3; } System.out.println("Largest among (" + n1 + ", " + n2 + ", " + n3 + ") is: " + largest); int smallest = n1; if (n2 < smallest) { smallest = n2; } if (n3 < smallest) { smallest = n3; } System.out.println("Smallest among (" + n1 + ", " + n2 + ", " + n3 + ") is: " + smallest); in.close(); } } |

Sample output of running the above program.

1 2 3 4 5 6 7 8 | Enter first number: 15 Enter second number: 562 Enter third number: 12 Largest among (15, 562, 12) is: 562 Smallest among (15, 562, 12) is: 12 |

## 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.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import java.util.Scanner; public class MaxMinNumber { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("Enter first number: "); int n1 = in.nextInt(); System.out.println("Enter second number: "); int n2 = in.nextInt(); System.out.println("Enter third number: "); int n3 = in.nextInt(); int largest = (n1 > n2) ? (n1 > n3 ? n1 : n3) : (n2 > n3 ? n2 : n3); System.out.println("Largest among (" + n1 + ", " + n2 + ", " + n3 + ") is: " + largest); int smallest = (n1 < n2) ? (n1 < n3 ? n1 : n3) : (n2 < n3 ? n2 : n3); System.out.println("Smallest among (" + n1 + ", " + n2 + ", " + n3 + ") is: " + smallest); in.close(); } } |

Output:

1 2 3 4 5 6 7 8 | Enter first number: 101 Enter second number: 15 Enter third number: 17 Largest among (101, 15, 17) is: 101 Smallest among (101, 15, 17) is: 15 |

## 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.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | import java.util.Scanner; public class MaxMinNumberInArray { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("Enter number of elements:"); int n = in.nextInt(); int numbers[] = new int[n]; System.out.println("Enter " + n + " elements one by one:"); for (int i = 0; i < n; i++) { numbers[i] = in.nextInt(); } // assign first element of an array to largest and smallest int smallest = numbers[0]; int largest = numbers[0]; // index starts from 1 since the first element need not be compared with itself for (int i = 1; i < numbers.length; i++) { if (numbers[i] > largest) { largest = numbers[i]; } else if (numbers[i] < smallest) { smallest = numbers[i]; } } System.out.println("Largest among the given numbers is: " + largest); System.out.println("Smallest among the given numbers is: " + smallest); in.close(); } } |

Sample output:

1 2 3 4 5 6 7 8 9 10 | Enter number of elements: 5 Enter 5 elements one by one: 25 6 1 8 10 Largest among the given numbers is: 25 Smallest among the given numbers is: 1 |

## 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.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import java.util.ArrayList; import java.util.Collections; import java.util.List; public class MaxMinInList { public static void main(String[] args) { List<Integer> numbers = new ArrayList<Integer>(); numbers.add(10); numbers.add(1); numbers.add(2); numbers.add(5); numbers.add(15); int largest = Collections.max(numbers); int smallest = Collections.min(numbers); System.out.println("Largest among the given numbers is: " + largest); System.out.println("Smallest among the given numbers is: " + smallest); // using stream largest = numbers.stream().max(Comparator.naturalOrder()).get(); smallest = numbers.stream().min(Comparator.naturalOrder()).get(); System.out.println("\nUsing Streams in Java 8"); System.out.println("Largest among the given numbers is: " + largest); System.out.println("Smallest among the given numbers is: " + smallest); } } |

Here is the output of running the above program.

1 2 3 4 5 6 | Largest among the given numbers is: 15 Smallest among the given numbers is: 1 Using Streams in Java 8 Largest among the given numbers is: 15 Smallest among the given numbers is: 1 |

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.

#### Latest posts by Uday (see all)

- How to run shell commands using SSH connection in Java - July 26, 2017
- Java program to print rectangle of stars - July 20, 2017
- Introduction to Java Enum data type with example programs - March 31, 2017