Write a Java program to find second lowest number from the array


Introduction

In this demo I have used NetBeans IDE 8.2 for debugging purpose. But you can use any java programming language compiler as per your availability..


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

  1. import java.util.Arrays;
  2. public class Javaexcercise {
  3. public static void main(String[] args) {
  4.  
  5. int[] myarray = {-1, 1, 2, 3, 5, 10};
  6. System.out.println("Original numeric array : "+Arrays.toString(myarray));
  7. int min = Integer.MAX_VALUE;
  8. int secondmin = Integer.MAX_VALUE;
  9. for (int i = 0; i < myarray.length; i++) {
  10. if(myarray[i]==min){
  11. secondmin=min;
  12. } else if (myarray[i] < min) {
  13. secondmin = min;
  14. min = myarray[i];
  15. } else if (myarray[i] < secondmin) {
  16. secondmin = myarray[i];
  17. }
  18.  
  19. }
  20. System.out.println("Second lowest number is : " + secondmin);
  21. }
  22. }

Result

java techstudy.org

     





Related Topics




Popular Posts