Largest And Smallest Number Program Using Java With I/O Stream

Introduction

In this blog, I will explain about the largest and smallest number in the given array program, using Java. It is very simple in Java Programming. The output will be displayed in the Run module.

Software Requirement

JDK1.3.

Simple Program

  1. import java.io.*;  
  2.   
  3. class large  
  4.  {  
  5.   public static void main(String arg[]) throws IOException   
  6.    {  
  7.      DataInputStream dr = new DataInputStream(System.in);  
  8.       
  9.      System.out.println("Enter the size of Array : ");  
  10.   
  11.      int no = Integer.parseInt(dr.readLine());  
  12.   
  13.      int a[]= new int[no];  
  14.   
  15.      System.out.println("Enter the Elements of Array...");  
  16.   
  17.        for(int i=0;i<no;i++)  
  18.         {  
  19.           a[i] = Integer.parseInt(dr.readLine());  
  20.         }  
  21.   
  22.        int small = a[0];  
  23.        int large = a[0];      
  24.   
  25.        for(int i=0;i<no;i++)  
  26.         {  
  27.          if(a[i] > large)  
  28.           large = a[i];  
  29.   
  30.          else if(a[i] < small)  
  31.           small = a[i];  
  32.         }   
  33.               
  34.          System.out.println("The Smallest Number of the given array is : "+ small);  
  35.   
  36.          System.out.println("The Largest Number of the given array is : "+ large);  
  37.    }  
  38.   }   
Explanation

In this blog, I will explain about the largest and smallest number in the given array program using Java. The output will be displayed in the Run module.

Output

 
Ebook Download
View all
Learn
View all