Java – Find Largest Number of an Array
A number array like integer array, float array, double array or long array can contain numbers with different values. We can find the largest number of these, in an array.
In this tutorial, we shall learn how to find the largest number of a given array using different looping statements in Java.
Example 1 – Find Largest Number of Array using While Loop
In this example, we shall use Java While Loop, to find largest number of given integer array.
Solution
- Take an integer array with some elements.
- Initialize a variable
largest
with the lowest of the integer value,Integer.MIN_VALUE
. This ensures that thelargest
picks the first element of the given array, in first iteration of the loop. - Take a variable
index
. We shall use it to access elements of the array. Initializeindex
with zero. We shall start from the left of array. - Write a while loop that executes when
index
is less than length of the integer array. Incrementindex
during each iteration.- Inside while loop, check if
largest
is less than this element. If so, then update thelargest
with this element. Iflargest
is not less than this element, we are happy with existing value oflargest
, nothing to do, so no else block required.
- Inside while loop, check if
- After the while loop execution is done, we end with the largest element of array, in the variable
largest
.
Java Program
/**
* Java Program - Find Largest Number of an Array
*/
public class LargestNumberArray {
public static void main(String[] args) {
//an array
int[] arr = {25, 86, 41, 97, 22, 34};
//initialize with smallest possible value
int largest = Integer.MIN_VALUE;
//find largest element of array
int index = 0;
while( index < arr.length ) {
//check if largest is smaller than element
if( largest < arr[index] ) {
//update largest
largest = arr[index];
}
index++;
}
System.out.println("The largest number is : "+ largest);
}
}
Output
Run the above Java program in your IDE or using Java command in command prompt.
The largest number is : 97
The program found out the largest integer in given integer array, as shown in the output.
Example 2 – Find Largest Number of Array using For Loop
In our previous example, we have taken an integer array. So, In this example, we shall take a float array and find largest floating point number using Java For Loop.
Solution
- Take a floating point array with some elements.
- Initialize a variable
largest
with the lowest of the Float value,Float.MIN_VALUE
. This ensures that thelargest
picks the first element of the given array, in first iteration of the loop. - Take a variable
index
. We shall use it to access elements of the array. Initializeindex
with zero in For Loop initialization part. We shall start from the left of array. - Write a for loop that executes when
index
is less than length of the integer array. Incrementindex
during each iteration.- Inside for loop, check if
largest
is less than this element. If so, then update thelargest
with this element. Iflargest
is not less than this element, we are happy with existing value oflargest
, nothing to do, so no else block required.
- Inside for loop, check if
- After the for loop execution is done, we end with the largest element of array, in the variable
largest
.
Java Program
/**
* Java Program - Find Largest Number of an Array
*/
public class LargestNumberArray {
public static void main(String[] args) {
//an array
float[] arr = {2.5f, 6.9f, 4.1f, 9.7f, 2.2f, 3.4f};
//initialize with smallest possible value
float largest = Float.MIN_VALUE;
//find largest element of array
for(int index = 0; index < arr.length; index++) {
//check if largest is smaller than element
if( largest < arr[index] ) {
//update largest
largest = arr[index];
}
}
System.out.println("The largest number is : "+ largest);
}
}
Output
Run the above Java Program in your IDE or command prompt using Java command.
The largest number is : 9.7
The program found the largest floating point number in given floating point array as shown in the output.
Example 3 – Find Largest Number of Array using Advanced For Loop
In this example, we shall take a double array and find largest number using Java Advanced For Loop.
Solution
- Take a double array with some elements.
- Initialize a variable
largest
with the lowest of the Double value,Double.MIN_VALUE
. This ensures that thelargest
picks the first element of the given array, in first iteration of the loop. - Write an advanced for loop that iterates over each element of the double array.
- Inside for loop, check if
largest
is less than this element. If so, then update thelargest
with this element. Iflargest
is not less than this element, we are happy with existing value oflargest
, nothing to do, so no else block required.
- Inside for loop, check if
- After the advanced for loop execution is done, we end with the largest element of array, in the variable
largest
.
Java Program
/**
* Java Program - Find Largest Number of an Array
*/
public class LargestNumberArray {
public static void main(String[] args) {
//an array
double[] arr = {2.5, 6.9, 4.1, 9.7, 2.2, 3.4};
//initialize with smallest possible value
double largest = Integer.MIN_VALUE;
//find largest element of array
for(double element : arr) {
//check if largest is smaller than element
if( largest < element ) {
//update largest
largest = element;
}
}
System.out.println("The largest number is : "+ largest);
}
}
Output
Run the above Java Program in your IDE or command prompt using Java command.
The largest number is : 9.7
The program found the largest double in given double array as shown in the output.
Conclusion
In this Java Tutorial, we learned how to find the largest number of a given array, using different looping statements in Java.