- Get link
- X
- Other Apps
In this blog, we will learn how to find the biggest number in an array in Java. Suppose you have given an array of integers as follows
int numbersArray[] = new int[]{10, 105, 21, 41, 68, 16, 95, 34, 32};
To find the biggest number first, we will assume that the first number is the biggest number in the array. In this case, it is '10'. Then using for-loop we will iterate through all the elements of the array and compare our assumed biggest number to each following number. If any element is found to be bigger than the assumed element then will replace the assumed biggest number with the current number. After iterating through the array we will get the biggest number.
Algorithm to find the biggest number in the array
Code to find the biggest number in the array
public class BiggestNumberInTheArray {
public static void main(String[] args) {
int biggestNumber;
int numbersArray[] = new int[]{10, 105, 21, 41, 68, 16, 95, 34, 32};
// Let's assume that the first number is the biggest number
biggestNumber = numbersArray[0];
for (Integer number : numbersArray) {
// Let's check whether the current number is bigger than
// the previous biggest number
if (number > biggestNumber) {
biggestNumber = number;
}
}
System.out.println("Biggest number : " + biggestNumber);
}
}
public static void main(String[] args) {
int biggestNumber;
int numbersArray[] = new int[]{10, 105, 21, 41, 68, 16, 95, 34, 32};
// Let's assume that the first number is the biggest number
biggestNumber = numbersArray[0];
for (Integer number : numbersArray) {
// Let's check whether the current number is bigger than
// the previous biggest number
if (number > biggestNumber) {
biggestNumber = number;
}
}
System.out.println("Biggest number : " + biggestNumber);
}
}
=====================
Output:
Biggest number : 105
=====================
Hope you understood how to find out the biggest number from an array list. If you still have any doubts then please let me know in the comment section. Happy coding!
Comments
Post a Comment