Q:

Java Program to find second smallest element

0

Program to find second smallest element using scanner for taking the input from the user

All Answers

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

import java.util.Scanner;
public class demo1
{
 public static void main(String[] args) 
 {    	
  int num, min;
  Scanner input = new Scanner(System.in);       
  System.out.print("Enter number of elements : ");
  num = input.nextInt();     
  int arr[] = new int[num];       
  System.out.println("Enter the elements in array : ");
        
  for (int a = 0; a < num; a++) 
  {
   arr[a] = input.nextInt();
  }
  for (int a = 0; a < num; a++) 
  {
   for (int b = a + 1; b < num; b++) 
   {
    if (arr[a] > arr[b]) 
    {
     min = arr[a];
     arr[a] = arr[b];
     arr[b] = min;
    }
   }
  }
  System.out.println("The Smallest element in the array is :"+arr[1]);
 }
}

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

total answers (1)

Similar questions


need a help?


find thousands of online teachers now