Wednesday, June 10, 2015

Java program to print two largest numbers

Java program to print maximum of two numbers:

package caveof;

public class Maxtwo {

public void twoMaxNumbers(int[] nums){
int maxone = 0;
int maxtwo = 0;
for(int n:nums){
if(maxone < n){
maxtwo = maxone;
maxone = n;
}
else if(maxtwo < n){
maxtwo = n;
}
}
System.out.println("First Max Number: "+maxone);
        System.out.println("Second Max Number: "+maxtwo);

}

public static void main(String[] args) {
// TODO Auto-generated method stub
int num[] = {5,34,78,2,45,1,99,23};
Maxtwo twomn = new Maxtwo();
twomn.twoMaxNumbers(num);

}
}

No comments:

Post a Comment