This is very simple program.
let arr[]={1,2,333,44}; out put will be 333,44 where 333 is first max number and 44 is second max number.
logic:just sort the array and return last two elements.[you can sort array by using any sort method ].Here am telling you a one line code for sorting.
---> java has a class called Arrays .you can use Arrays.sort(array) to sort elements in user defined array.
program:
package logics;
import java.util.*;
public class twoMax {
public static void main(String []a )
{
int arr[]=new int[]{1,22,33,222,0,66};
Arrays.sort(arr);
int firstMax=arr[arr.length-1];
int secondMax=arr[arr.length-2];
System.out.print("first max is"+firstMax+"\n second max is"+secondMax);
}
}
output:
---------------------------
first max is222
second max is66
let arr[]={1,2,333,44}; out put will be 333,44 where 333 is first max number and 44 is second max number.
logic:just sort the array and return last two elements.[you can sort array by using any sort method ].Here am telling you a one line code for sorting.
---> java has a class called Arrays .you can use Arrays.sort(array) to sort elements in user defined array.
program:
package logics;
import java.util.*;
public class twoMax {
public static void main(String []a )
{
int arr[]=new int[]{1,22,33,222,0,66};
Arrays.sort(arr);
int firstMax=arr[arr.length-1];
int secondMax=arr[arr.length-2];
System.out.print("first max is"+firstMax+"\n second max is"+secondMax);
}
}
output:
---------------------------
first max is222
second max is66
Không có nhận xét nào:
Đăng nhận xét