Example: #1 - Check the number is a Palindrome
Check the number is a Palindrome using a divide and modulus approach
Groovy Input Screen
class CheckPalindrome {
public static void main(String[] args) {
def originalNumber = 12321
def remainder
def reverseNumber = 0
def temp = originalNumber
while (temp > 0) {
remainder = temp % 10
reverseNumber = (reverseNumber * 10) + remainder
temp = temp.intdiv(10)
}
if (originalNumber == reverseNumber) {
println("The given number is Palindrome")
} else {
println("The given number is NOT a Palindrome")
}
}
}
Groovy Output Screen
The given number is Palindrome