Write a program to find smallest positive integer in an array

Write a program in java to find the smallest positive number in an array

class SmallestNM
{

   public static void main(String args[])
     {
     int array[] = {-15,-5,0,2,3,4,-8,9};
     int smallest=0;
        for(int i=0;i<array.length;i++) // Find the first number in array>0 
        {
            if(array[i]>0)
            {
                smallest=array[i]; 
                break;// Break out of loop, when you find the first number >0
            }   
        }

        for(int i=0;i<array.length;i++) // Loop to find the smallest number in array[]
        {
            if(smallest>array[i]&&array[i]>0)
            {
                smallest=array[i];
            }
        }
        System.out.println(smallest);
        }
     }
}

Output:  2

Posted on by