programming-examples/java/Basics/Program to Find Second smallest number in java without sorting (Java).java

26 lines
723 B
Java
Raw Normal View History

2019-11-18 14:44:36 +01:00
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);
}
}