Here’s a Java program to check whether a given number is a palindrome or not:
import java.util.Scanner;
public class PalindromeNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
int reversedNum = 0;
int originalNum = num;
while (num != 0) {
int remainder = num % 10;
reversedNum = reversedNum * 10 + remainder;
num = num / 10;
}
if (originalNum == reversedNum) {
System.out.println(originalNum + " is a palindrome number");
} else {
System.out.println(originalNum + " is not a palindrome number");
}
}
}
In this program, we first import the Scanner
class from the java.util
package to read input from the user.
We then prompt the user to enter a number using the System.out.print()
function and read the input using the nextInt()
function of the Scanner
class.
Next, we declare two integer variables reversedNum
and originalNum
and initialize originalNum
to the entered number. We then use a while
loop to reverse the entered number and store the reversed number in the reversedNum
variable.
Finally, we compare the original number with the reversed number to determine whether it is a palindrome or not, and display the result using the System.out.println()
function.
When you run this program and enter a number, the output will be:
Enter a number: 121
121 is a palindrome number
If you enter a non-palindrome number like 123, the output will be:
Enter a number: 123
123 is not a palindrome number
This program is a basic example of how to check if a given number is a palindrome or not using Java.