FindLargest and Smallest Number in an Array Example /*   Find Largest and Smallest Number in an Array Example   This Jav...

FindLargest and Smallest Number in an Array Example

00:03 PASSOVE INCOME 0 Comments

FindLargest and Smallest Number in an Array Example

  1. /*
  2.   Find Largest and Smallest Number in an Array Example
  3.   This Java Example shows how to find largest and smallest number in an
  4.   array.
  5. */
  6. public class FindLargestSmallestNumber {
  7.         public static void main(String[] args) {
  8.                
  9.                 //array of 10 numbers
  10.                 int numbers[] = new int[]{32,43,53,54,32,65,63,98,43,23};
  11.                
  12.                 //assign first element of an array to largest and smallest
  13.                 int smallest = numbers[0];
  14.                 int largetst = numbers[0];
  15.                
  16.                 for(int i=1; i< numbers.length; i++)
  17.                 {
  18.                         if(numbers[i] > largetst)
  19.                                 largetst = numbers[i];
  20.                         else if (numbers[i] < smallest)
  21.                                 smallest = numbers[i];
  22.                        
  23.                 }
  24.                
  25.                 System.out.println("Largest Number is : " + largetst);
  26.                 System.out.println("Smallest Number is : " + smallest);
  27.         }
  28. }
  29. /*
  30. Output of this program would be
  31. Largest Number is : 98
  32. Smallest Number is : 23
  33. */

0 comments: