6. Write a program to find max, min from an array

public class Main {
    public static void main(String[] args) {
        int[] numbers={14,12,9,15,17,21,23,8,5,25};
        int max=findMax(numbers);
        int min=findMin(numbers);
        System.out.println("Max number is: "+max);
        System.out.println("Min number is: "+min);
    }
    public static int findMax(int arr[]){
        int max=0;
        for(int i=0;i<arr.length;i++){
            if(arr[i]>arr[0]){
                max=arr[i];
            }
        }
        return max;
    }
    public static int findMin(int arr[]){
        int min=0;
        for(int i=0;i<arr.length;i++){
            if(arr[i]<arr[0]){
                min=arr[i];
            }
        }
        return min;
    }

}
Output:
Max number is: 25
Min number is: 5

about author

admin

salmansrabon@gmail.com

If you like my post or If you have any queries, do not hesitate to leave a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *