-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPalindromeTest.java
More file actions
44 lines (39 loc) · 1.01 KB
/
PalindromeTest.java
File metadata and controls
44 lines (39 loc) · 1.01 KB
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
package com.Strings;
public class PalindromeTest {
/**
* Test the actual code if it works correctly
*/
public static void main(String[] args) {
System.out.println(checkIntegerPalindrome(100)); // false
System.out.println(checkIntegerPalindrome(101)); // true
System.out.println(checkIntegerPalindrome(500045)); // false
System.out.println(checkIntegerPalindrome(50005)); // true
}
/**
* This function will test the equality if a number and its reverse.
*
* @return true if number is palindrome else false
*/
public static boolean checkIntegerPalindrome(int number) {
boolean isPalindrome = false;
if (number == reverse(number)) {
isPalindrome = true;
}
return isPalindrome;
}
/**
* This function will reverse a given number.
*
* @return reverse number
*/
public static int reverse(int number) {
int reverse = 0;
int remainder = 0;
do {
remainder = number % 10;
reverse = reverse * 10 + remainder;
number = number / 10;
} while (number > 0);
return reverse;
}
}