belongs to collection: Array Programs in Java with Examples
Write a Java program to find second lowest number from the array
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
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
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..
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