Java Program to find the Second Smallest Number in the Array
Get array size n and n elements of array, then find the second largest element among those elements.
Sample Input 1:
5
5 7 9 3 1
Sample Output 1:
7
Try your Solution
Strongly recommended to Solve it on your own, Don't directly go to the solution given below.
public class Hello
{
public static void main(String args[])
{
//Write your code here
}
}
Program or Solution
//To Find The Second Smallest Element In The Array...
import java.util.*;
class Program
{
public static void main(String args[])
{
int size,i;
Scanner sc=new Scanner(System.in);
System.out.println("Enter The Size Of The Array:");
size=sc.nextInt();
int a[]=new int[size];
int c[]=new int[size-1];
System.out.println("Enter The Array Elements:");
for(i=0;i<size;i++)
{
a[i]=sc.nextInt();
}
int min=a[0];
for(i=0;i<size;i++)
{
if(min>a[i])
{
min=a[i];
}
}
System.out.println("The First Smallest Element In The Array Is:"+min);
int j=0;
for(i=0;i<size;i++)
{
if(a[i]>min)
{
c[j]=a[i];
j++;
}
}
int min1=c[0];
for(i=0;i<size-1;i++)
{
if(min1>c[i])
{
min1=c[i];
}
}
System.out.println("The Second Smallest Element In The Array Is:"+min1);
}
}
Program Explanation
Comments
Related Programs
- Java Program to get and print the array elements
- Java Program to find the sum of array elements.
- Java Program to find the average of array Elements
- Java Program to Search an Element in an array
- Java Program to find Largest element in the array
- Java Program to find Smallest element in the array
- Java Program to print all the numbers which are less than given key element from a given array.
- Java Program to Sort the Elements in ascending order.
- Java Program to Sort the Elements in descending order.
- Addition of Matrix in Java
- Java Program to delete an Element from the Array
- Java Program to merge two Arrays
- Java Program to Multiply two Matrices
- Java Program to Reverse the Elements of Array
- Java Program to Reverse the First Half Elements of Array
- Java Program to Reverse the Second Half Elements of Array
- Java Program to find the Second Largest Number in the Array
- Java Program to transpose the Matrix
coming Soon
coming Soon