In this HackerRank Java Lambda Expressions problem solution, you need to Write the following methods that return a lambda expression performing a specified action:

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

HackerRank Java Lambda Expressions problem solution

 HackerRank Java Lambda Expressions problem solution

 Write your code here
public static performOperation checkEvenOdd() {
return evenOdd;
}
public static performOperation checkPrime() {
return prime;
}
public static performOperation checkPalindrome() {
return palindrome;
}

public static performOperation evenOdd = (num) -> {

if(num % 2 == 0)
return 0;
else return 1;

};

public static performOperation palindrome = (num) -> {

if (num < 0)
return 1;
int div = 1;
while (num / div >= 10) {
div *= 10;
}
while (num != 0) {
int l = num / div;
int r = num % 10;
if (l != r)
return 1;
num = (num % div) / 10;
div /= 100;
}
return 0;

};

public static performOperation prime = (num) -> {

for(int i=2;i<num;i++) {
if(num%i==0)
return 1;
}
return 0;

};

}