Thursday, 18 January 2018

Write a program to find Smallest and Largest Number In Array In Java

package com.tutorial4u;

public class LargestAndSmallestArray {
     public static void main(String[] args) {
          
           int arraylist[] = {12,34,21,24,54,32,76};
           int smallestArray = arraylist[0];
           int largestArray = arraylist[0];
          
           for(int i =1 ;i< arraylist.length;i++){
               
                if(arraylist[i] > largestArray){
                      largestArray = arraylist[i];
                }else if(arraylist[i] < smallestArray){
                      smallestArray = arraylist[i];
                }
           }
           System.out.println("Smallest Number in Array is : "+smallestArray);
           System.out.println("Largest Numbe rin Array is : "+largestArray);
     }

}




Output :


Smallest Number in Array is : 12
Largest Numbe rin Array is : 76

No comments:

Post a Comment

No of Occurrence in Array

package com.tutorial4u; import java.util.HashMap; /************************************************************  No of Occurrence in Array...