-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolutionLambda.java
72 lines (70 loc) · 2.4 KB
/
SolutionLambda.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package basicjavaprograms;
import java.io.*;
import java.util.*;
import java.util.function.Predicate;
public class SolutionLambda {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int num =Integer.parseInt(scan.nextLine()) ;
for(int i=0;i<num;i++){
String str = scan.nextLine();
String[] sep = str.split(" ");
int testCase = Integer.valueOf(sep[0]);
int number = Integer.valueOf(sep[1]);
switch(testCase) {
case 1:
if(isOdd(number,n -> n%2==0))
System.out.println("EVEN");
else
System.out.println("ODD");
break;
case 2:
java.util.List<Integer> a = Arrays.asList(2,3,5,7,11);
if(number!=0 && number!=1) {
if(a.contains(number)) {
System.out.println("PRIME");
}
else if(isPrime(number, n ->(n%2!=0 && n%3!=0 && n%5!=0&& n%7!=0 && n%11!=0) ))
System.out.println("PRIME");
else
System.out.println("COMPOSITE");
}break;
case 3:
if(number<10) {
System.out.println("PALINDROME");
}else {
StringBuilder s = new StringBuilder(String.valueOf(number));
StringBuilder rev = new StringBuilder(s).reverse();
System.out.println(s+" "+rev);
if(isPalindrome(s.toString(),n -> n.toString().equals(rev.toString())))
System.out.println("PALINDROME");
else
System.out.println("NOT A PALINDROME");
}break;
default:
System.out.println("No Service");
}
}
}
public static boolean isOdd(int num, Predicate<Integer> p){
if(p.test(num)){
return true;
}
else
return false;
}
public static boolean isPrime(int num, Predicate<Integer> p){
if(p.test(num)){
return true;
}
else
return false;
}
public static boolean isPalindrome(String num, Predicate<String> p){
if(p.test(num)){
return true;
}
else
return false;
}
}