This content originally appeared on DEV Community and was authored by Shelner
Solving Code
public class Palindrome {
public boolean isPalindrome(int x) {
if (x < 0) return false;
if (x != 0 && x % 10 == 0) return false;
int reverse = 0;
while (x > reverse) {
int lastDigit = x % 10;
reverse = reverse * 10 + lastDigit;
x = x / 10;
}
return (x == reverse) || (x == reverse / 10);
}
}
Test Code (JUnit)
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class PalindromeTest {
private final Palindrome palindrome = new Palindrome();
@Test
void testSimpleIsPalindrome() {
assertTrue(palindrome.isPalindrome(12321));
}
@Test
void testFalseIsPalindrome() {
assertFalse(palindrome.isPalindrome(12312));
}
@Test
void testOnlyZero() {
assertTrue(palindrome.isPalindrome(0));
}
@Test
void testTen() {
assertFalse(palindrome.isPalindrome(10));
}
}
This content originally appeared on DEV Community and was authored by Shelner
Print
Share
Comment
Cite
Upload
Translate
Updates
There are no updates yet.
Click the Upload button above to add an update.
APA
MLA
Shelner | Sciencx (2025-11-01T05:35:30+00:00) Palindrome Number. Retrieved from https://www.scien.cx/2025/11/01/palindrome-number/
" » Palindrome Number." Shelner | Sciencx - Saturday November 1, 2025, https://www.scien.cx/2025/11/01/palindrome-number/
HARVARDShelner | Sciencx Saturday November 1, 2025 » Palindrome Number., viewed ,<https://www.scien.cx/2025/11/01/palindrome-number/>
VANCOUVERShelner | Sciencx - » Palindrome Number. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/11/01/palindrome-number/
CHICAGO" » Palindrome Number." Shelner | Sciencx - Accessed . https://www.scien.cx/2025/11/01/palindrome-number/
IEEE" » Palindrome Number." Shelner | Sciencx [Online]. Available: https://www.scien.cx/2025/11/01/palindrome-number/. [Accessed: ]
rf:citation » Palindrome Number | Shelner | Sciencx | https://www.scien.cx/2025/11/01/palindrome-number/ |
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.