Q:

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

belongs to collection: Array Programs in Java with Examples

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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..

import java.util.Arrays;
public class Javaexcercise {
  public static void main(String[] args) {
 
    int[] myarray = {-1, 1, 2, 3, 5, 10};
    System.out.println("Original numeric array : "+Arrays.toString(myarray));
    int min = Integer.MAX_VALUE;
    int secondmin = Integer.MAX_VALUE;
    for (int i = 0; i < myarray.length; i++) {
        if(myarray[i]==min){
          secondmin=min;
        } else if (myarray[i] < min) {
            secondmin = min;
            min = myarray[i];
        } else if (myarray[i] < secondmin) {
            secondmin = myarray[i];
        }
 
    }
    System.out.println("Second lowest number is : " + secondmin);
    }
}

Result:

Original numeric array : [-1, 1, 2, 3, 5, 10]

Second lowest number is : 1

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Array Programs in Java with Examples

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a Java program to find the number of even an... >>
<< Write a Java program to find second largest number...