Write a java program that inputs an integer and outputs if the number is a Palindrome.
public void palindrome(int num){
int reversedNumber = 0;
int temp;
// store the number to originalNumber
int originalNumber = num;
// get the reverse of originalNumber
// store it in variable
while (num != 0) {
temp = num % 10;
reversedNumber = reversedNumber * 10 + temp;
num /= 10;
}
// check if reversedNumber and originalNumber are equal
if (originalNumber == reversedNumber) {
System.out.println(originalNumber + " is Palindrome.");
}
else {
System.out.println(originalNumber + " is not Palindrome.");
}
}
Comments
Leave a comment