Wednesday 8 June 2016

Print Even or Odd without using conditional statement in Java



public class EvenOddWoCondition {

     /**
      * Return even odd result.
      * @param number
      * @return even or odd.
      */
     private static String getEvenOddRest(int number) {
           String[] array  = {"even","odd"};

           int index = number%2;

           return array[index];
     }

     public static void main(String[] args) {
           int number = 10;
           String result = getEvenOddRest(number);
           System.out.println(result);
     }
}

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...