- Write a Java program to check whether a number is palindrome or not?
Answer:- So, First let's see what is palindrome. Palindrome is a phrase, word, number or sequence of characters that should be the same reverse as well.
For example:-
The word "radar" and the reverse of the word is also the same(radar). So Word radar is a palindrome string.
Number 12321 and the reverse of the number are also the same so 12321 is a palindrome number.
Let's write a logic to check whether the inserted number is palindrome or not using Java.
package com.codeforsolution.java.logical;
import java.util.Scanner;
public class PalindromeCheck {
public static void main(String[] args) {
int number, reverse = 0, tempNumber, remainder;
System.out.println("Enter a number");
Scanner s = new Scanner(System.in);
number = s.nextInt();
s.close();
tempNumber = number;
while (number != 0) {
remainder = number % 10;
reverse = reverse * 10 + remainder;
number = number / 10;
}
if (tempNumber == reverse) {
System.out.println("Number is palindrome");
} else {
System.out.println("Number is not palindrome");
}
}
}
Output:-
1. Enter a number
151
Number is palindrome
2. Enter a number
134
Number is not palindrome
Here is the link to a YouTube video for the same program:- Palindrome Check program