Find Largest in Array

This program loops through every element of the array and if the current element of the array has a greater value than the variable ‘max’ then ‘max’ is updated to the value of the current element of the array. After the last iteration of the loop we get the required result which is the maximum among the given array.

01     public class Largest {
02
03         public static void main(String arguments[]){
04         int array[] = { 2 , 1 , 9 , 35 , 99 , 0 };
05         int max = 0;
06
07         System.out.println(“The Elements of the array are :”);
08         for(int k = 0;k < array.length; k++  ){
09             System.out.println(array[k]);
10         }
11
12         for(int i = 0;i < array.length;i++){
13             if( array[i] > max ){
14                 max = array[i];
15             }
16         }
17             System.out.println(“The largest element is : “+max);
18
19         }
20     }

Output:      largest

If you have any questions feel free to email me at daniequreshi@gmail.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s