Find maximum and minimum elements in array in java

Write a java program to find maximum and minimum elements in array.

package array;

import java.util.*;
public class maxminofarray 
{
    public static void main(String args[])
    {
        Scanner s=new Scanner(System.in);
        int size;
        System.out.println("enter size");
        size=s.nextInt();
        int a[]=new int[size];
        for(int i=0;i<size;i++)
        {
            a[i]=s.nextInt();
        }
        int max=a[0];
        for(int j=0;j<size;j++)
        {
            if(max<a[j])
            {
                max=a[j];
            }
        }
        System.out.println("max is"+max);
        int min=a[0];
        for(int j=0;j<size;j++)
        {
            if(min>a[j])
            {
                min=a[j];
            }
        }
        System.out.println("min is"+min);
    }
}

Leave a Reply

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