JAVA program to add top two maximum number of an Array.

Below program i have written to add top two max number of an Array:

package com.company;

public class ArrayDemo {


public int addAge(int [] age){

int largest = age[0];
int secondlargest = age[0];

for (int i=1;i<age.length;i++){

if ( age[i] > largest ){
secondlargest = largest;
largest = age[i];
}
if (age[i] < largest && age[i] > secondlargest){
secondlargest = age[i];
}

}


System.out.println("largest number is = " +largest);

System.out.println("Second largest number is = " +secondlargest);
int total = largest + secondlargest;
return total;
}


public static void main (String[] args){

int [] age = {12,88,13,4,15,25,5,49,87};

ArrayDemo arrayAddition = new ArrayDemo();

int total = arrayAddition.addAge(age);

System.out.println(total);


}
}

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s