Find Largest and Smallest Number in Array

Price: INR 8,999.00
Price: INR 15,999.00
Was: INR 16,999.00

Let’s see, How can you find largest and smallest number in Array. Understanding of array data structure is very important for any software developer or Automation Test Engineer.

Find smallest and largest number in the Array is one of favorite Interview question in Java Programming. In this program, you need to write a method, which will accept an integer array and then print largest and smallest number from that array.

As a bonus point Interviewer may ask to write the Unit Testing code for your implemented method, then you need to  write JUnit test cases to test your method.

 

Find Largest and Smallest Number in Array

Let’s see the code find the smallest and largest number in the array. To implement the code, We will use below-mentioned algorithm –

  1. Declare the Array.
  2. Declare two variables i.e smallest & Largest and save the value of array first element in these variables.
  3. Now Iterate over the Array and compare the Array element with largest variables.
  4. If the array element is greater than the value of largest variable then save the array value in the largest variable.
  5. Put another condition in another if clause and compare the Array Element with the Smallest variable.
  6. If ArrayElement value is less than the value of Smallest variable then assign this to the smallest variable.

That’s all, Let’s see the implementation of above algorithm –

Output :

Largest Number is : 125
Smallest Number is : 8

 

That’s all about How to find largest and smallest number from integer array in Java.

 

0 Comment

Leave a Reply