programming-examples/java/_Basics/Program to Find Second smallest number in java without sorting (Java).java
2019-11-15 12:59:38 +01:00

26 lines
723 B
Java

class SecondSmallestNumber
{
public static void main(String args[])
{
int numbers[] = {6,3,37,12,46,5,64,21};
int smallest= numbers[0];;
int second_smallest = numbers[0];;
for(int n:numbers)
{
if(n< smallest)
{
second_smallest = smallest;
smallest=n;
}
else if( n< second_smallest)
{
second_smallest= n;
}
}
System.out.println("Smallest Number: "+smallest);
System.out.println("Second Smallest Number: "+second_smallest);
}
}