What is a Palindrome Number?
Palindrome number is a number which remains same even after reversing its digits.
Case | Examples |
Palindrome Number | 121, 2332, 34543, etc. |
Non-Palindrome Number | 122, 2335, etc |
Palindrome Number in Java
Let’s print palindrome number list using a for loop.
package com.techstackjournal.java.basics;
public class Palindrome {
public static void main(String[] args) {
for (int i = 0; i < 500; i++) {
if (isPalindrome(i)) {
System.out.printf("%d ",i);
}
}
}
private static boolean isPalindrome(int number) {
int reminder;
int reverseNum = 0;
int tempNum = number;
while (tempNum != 0) {
reminder = tempNum % 10;
reverseNum = reverseNum * 10 + reminder;
tempNum = tempNum / 10;
}
return number == reverseNum;
}
}
Palindrome Number in C language
#include <stdio.h>
short isPalindrome(int number)
{
int reminder;
int reverseNum = 0;
int tempNum = number;
while (tempNum != 0)
{
reminder = tempNum % 10;
reverseNum = reverseNum * 10 + reminder;
tempNum = tempNum / 10;
}
return number == reverseNum;
}
void main()
{
for (int i = 0; i < 500; i++)
{
if (isPalindrome(i))
{
printf("%d ",i);
}
}
}