Once you finish the iteration, you have the largest and smallest number without using an array. You can also assign the largest number as zero if there is no negative number in your input, but it won't work if the user can enter both negative and positive integers.
To make this program more interactive, you can accept all N numbers from the user from the command line by using Scanner. The user will keep entering an integer number on each iteration and you would be printing the largest and smallest number.
Alternatively, you can get all N numbers in a List and loop through that list to display largest and smallest number.
Java Program to find largest and smallest of N numbers without arrays
Here is our sample program to find the smallest and largest of N integers without using an array. This program handles both positive and negative number, hence largest value is initialized with Integer.MIN_VALUE and smallest number are initialized with Integer.MAX_VALUE.If you are sure that your input will only be a positive number then you can initialize the largest with zero instead of Integer.MIN_VALUE. The program is simple, just take input from the user about how many numbers and then uses a for loop to get that many numbers as input by using Scanner.nextInt() method.
At the same time, we also keep comparing the largest and smallest numbers with the value entered by the user, so that at the end of the loop we have the largest and smallest number from the set of values entered by users.
Btw, if you are preparing coding problems for interviews, to get your first job or looking for a new job, you should check out the Cracking the Code Interview, it contains around 190 programming questions and their solution from various tech interviews. It will give you both common questions and experience on how to solve them.

Program to print largest and smallest of N numbers
import java.util.Scanner; /* * Java Program to find the largest and smallest of N numbers * without using arrays. */ public class LargestOfN { public static void main(String[] args) { System.out.println("Welcome to Java Program to find " + "largest and smallest number without using array"); System.out.println("Please enter value of N: "); Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int largest = Integer.MIN_VALUE; int smallest = Integer.MAX_VALUE; System.out.printf("Please enter %d numbers %n", n); for (int i = 0; i < n; i++) { int current = sc.nextInt(); if (current > largest) { largest = current; } if (current < smallest) { smallest = current; } } System.out.println("largest of N number is : " + largest); System.out.println("smallest of N number is : " + smallest); } }
You can see that our program has correctly printed the largest and smallest number from the 10 numbers entered by the user. You can also run this program and further check with 20, 30 or any value of N. You can also enter negative numbers to verify if its is calculating largest and smallest correctly with negative values or not.
That's all about how to calculate largest and smallest value in N numbers without using arrays. It's an interesting exercise and teaches you how to use the if-else statements to solve the problem. If you are a beginner programmer and doing these exercise to build your code sense and programming logic, you can also check out following problems for further practice.
Some more array based coding problems for Programmers
- How to find the missing number in an array of 1 to 100? (solution)
- How to find duplicate numbers on integer array? (solution)
- How to find largest and smallest number in the unsorted array? (solution)
- How to reverse an array in place in Java? (solution)
- How to find all pairs in integer array whose sum is equal to given number? (solution)
- How to find duplicates if an array contains multiple duplicate elements? (solution)
- How to remove duplicate elements from the array in Java? (solution)
- How to find the top two numbers from an int array? (solution)
- How to remove duplicate elements from the array in place? (solution)
- How to sort an array using Quicksort algorithm? (solution)
No comments:
Post a Comment