Write the following methods that return a lambda expression performing a specified action:

  1. PerformOperation isOdd(): The lambda expression must return  if a number is odd or  if it is even.
  2. PerformOperation isPrime(): The lambda expression must return  if a number is prime or  if it is composite.
  3. PerformOperation isPalindrome(): The lambda expression must return  if a number is a palindrome or  if it is not.

Input Format

Input is handled for you by the locked stub code in your editor.

Output Format

The locked stub code in your editor will print  lines of output.


hackerrank java lambda expressions solution


Java Lambda expressions problem solution | HackerRank

class MyMath {
 public static boolean checker(PerformOperation p, int num) {
  return p.check(num);
 }
 public PerformOperation isOdd() {
  return (a) -> {
   return (a % 2 == 1);
  };
 }
 public PerformOperation isPrime() {
  return (a) -> {
   for (int i = 2; i <= a / 2; i++) {
    if (a % i == 0)
     return false;
   }
   return true;
  };
 }
 public PerformOperation isPalindrome() {
  return (a) -> {
   int rev = 0;int r = 0;int n = a;
   while (n != 0) {
    r = n % 10;
    rev = rev * 10 + r;
    n /= 10;
   }
   return (rev == a);
  };
 }

}