// check for a number is even or odd without / or % operator.
public class EvenOrOdd {
public static int CheckEvenOrOdd(int num) {
if (num > 2) {
int number = num - 2;
num = CheckEvenOrOdd(number);
}
return num;
}
public static void main(String[] args) {
int num = CheckEvenOrOdd(5322221);
if (num == 1) {
System.out.println("Odd number");
} else {
System.out.println("Even number");
}
}
}
I defined stack size to 200m as -xss200m but this program turn up with OutOfMemory error and StackOverflow error if number is greater then 5322221.
Advise how to solve this problem to find the number is even or odd.