Find Largest And Smallest Number Using Array In Java

Ram Pothuraju
//File name--> MinMax.java
import java.io.*;

class MinMax{
 public static void main(String args[])throws Exception
 {
  BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  int n,min=0,max=0;
 
  System.out.println("Enter number of element you want in array.");
  n=Integer.parseInt(br.readLine());
  int[] a=new int[n];
 
  for(int i=0;i < n;i++)
  {
   System.out.println("Enter elementa["+i+"]:");
   a[i]=Integer.parseInt(br.readLine());
  }
  min=max=a[0];
  System.out.print("Content of Array:");
  for(int i=0;i < n;i++)
  {
   System.out.print("\t"+a[i]);
   
   if(min>a[i])
    min=a[i];
   
   if(max < a[i])
    max=a[i];
  }
  System.out.println("\nLargest element from array is:"+max);
  System.out.println("Smallest element from array is:"+min);
 }
}





OUTPUT

javac MinMax.java

java MinMax
Enter number of element you want in array.
10
Enter elementa[0]:
-5
Enter elementa[1]:
11
Enter elementa[2]:
88
Enter elementa[3]:
77
Enter elementa[4]:
66
Enter elementa[5]:
55
Enter elementa[6]:
44
Enter elementa[7]:
33
Enter elementa[8]:
22
Enter elementa[9]:
11
Content of Array:       -5      11      88      77      66      55      44
33      22      11
Largest element from array is:88
Smallest element from array is:-5

Post a Comment

0Comments

Post a Comment (0)