Advertisement
kamandos

HW4

Jun 29th, 2022
577
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.01 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main
  4. {
  5.  
  6.      public static void main(String []args)
  7.      {
  8.          Scanner sc=new Scanner(System.in);
  9.          int n;      
  10.          System.out.println("Enter the size of the array");
  11.          n=sc.nextInt();    
  12.          
  13.          int arr[]=new int[n];    
  14.         System.out.println("Enter the array");  
  15.         for(int i=0;i<n;i++)      
  16.         {
  17.             arr[i]=sc.nextInt();
  18.         }
  19.                
  20.         for(int i=0;i<n;i++)    
  21.         {
  22.             for(int j=i+1;j<n;j++)    
  23.             {
  24.         //Checking and swap
  25.                 if(arr[i]<arr[j])    
  26.                 {
  27.                     int temp=arr[i];
  28.                     arr[i]=arr[j];
  29.                     arr[j]=temp;
  30.                 }
  31.             }
  32.         }
  33.        
  34.         System.out.println("Second Largest element is "+arr[1]);   //Display second largest element.
  35.         System.out.println("Second Smallest element is "+arr[n-2]);  //Display second smallest element.
  36.     }
  37.  
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement