52. JAVA Program To find the Second Smallest Number among the given numbers.
How does this program work?
- This code helps to find Second Smallest number from the given numbers using java.
Here is the code
//To find Second smallest number in Array
import java.util.Scanner;public class small
{
public static void main(String[] args )
{
// Intialising the variables
int n, min;// Enter the number of elements.
System.out.print("Enter number of elements : ");Scanner skill = new Scanner(System.in);
n = skill.nextInt();
// creating an array.
int a[] = new int[n];// enter array elements.
System.out.println("Enter the elements in array : ");for (int i = 0; i < n; i++)
{
a[i] = skill.nextInt();
}
for (int i = 0; i < n; i++)
{
for(int j = i + 1; j < n; j++)
{
if (a[i] > a[j])
{
min = a[i];
a[i] = a[j];
a[j] = min;
}
}
}
System.out.println("The SecondSmallest element in the array is :"+a[1]);
}
}