Write a Java program to find second largest 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. int[] myarray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  5. System.out.println("Original numeric array : "+Arrays.toString(myarray));
  6. Arrays.sort(myarray);
  7. int index = myarray.length-1;
  8. while(myarray[index]==myarray[myarray.length-1]){
  9. index--;
  10. }
  11. System.out.println("Second largest element of the array: " + myarray[index]);
  12. }
  13. }

Result

java techstudy.org

     





Related Topics




Popular Posts