Find largest and smallest element in array in java.

Find largest and smallest element in array using java.
 Write java code to finding the largest and smallest number in a given array.
import java.io.*;
public class bigsmallfind
{
                public static void main(String[]args)throws IOException
                {
                                int size,i,big=0,small=99999;
                                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                                System.out.print("enter the size of array:");
                                size=Integer.parseInt(br.readLine());
                                int a[]=new int[size];
                                System.out.print("enter the elements of array:");
                                for(i=0;i<size;i++)
                                {
                                                a[i]=Integer.parseInt(br.readLine());
                                               
                                }
                                for(i=0;i<size;i++)
                                {
                                                if(big<a[i])
                                                                big=a[i];
                                }
                                System.out.println("Largest number is:"+big);
                                for(i=0;i<size;i++)
                                {
                                                if(small>a[i])
                                                                small=a[i];
                                }
                                System.out.println("smallest number is:"+small);
                }
 
}

No comments:

Post a Comment