Wednesday, 15 June 2016

Program to find the least among 4 numbers.

If 20,33,5,77 are the given numbers then 5 must be returned.

Code:

public class LeastNumberFrom4
{
    public static void main(String[] args)
   {
        int n1 = 20;
        int n2 = 33;
        int n3 = 5;
        int n4 = 77;
        System.out.println(getLeastNumber(n1, n2, n3, n4));
    }

    public static int getLeastNumber(int num1 , int num2, int num3, int num4)
   {
         int least;
         int[] n = {num1,num2,num3,num4};
         least = Integer.MAX_VALUE;
         for(int i =0;i<n.length;i++)
         {
            if(least > n[i])
             {
                        least = n[i];
              }
          }
             return least;
     }
 }

No comments:

Post a Comment