Misc-Java-Examples

How to find the largest number in an array

Posted On
Posted By admin

In this blog post, I will be demonstrating how you can find the largest number in an array or list. Consider the following code snippet:


package demo;

public class FindLargestNumberDemo {

public static void main(String[] args) {
int myArray[] = {2,4,6,8,10,45,9,18,90,12};

int largest = 0;
for(int num:myArray) {
if(num > largest)
largest = num;
}
System.out.println("Largest number is "+largest);

}

}

 

Here, we are declaring an array with a list of values. We are then iterating through the elements in the array. We are also declaring a variable called largest and setting its value to 0. Each element in the array is compared to the variable largest. If is is greater than largest, the variable largest is assigned the current element. This is continued for all the elements in the input array. So when this code is executed, it will print the following output:

 

 

If you'd like to watch a detailed video tutorial of this topic or other related topics, do check out my Java course here

Also, if you'd like to test your Java knowledge, do check out my practice tests course here


If you like this post, please do let me know via the comments box below.  You can also connect with me via my Facebook Page or subscribe to my Youtube channel!

Related Post

leave a Comment