Java Program to Check if a Number is Palindrome - The Coding Shala
Home >> Java Programs >> Palindrome Number
Other Posts You May Like
In this post, we will learn how to Check if a Number is Palindrome or not in Java.
Java Program to Check if a Number is Palindrome
A Palindrome number is a number that is the same after reversing the number. For example, number 121 is a palindrome number because the reverse of 121 is 121. Write a Java Program to Check if the Given Number is Palindrome or Not.
Example 1:
Input: 545
Output: Palindrome
Input 2:
Input: 234
Output: Not a Palindrome
Approach
First, reverse the number then check if it is equal to the original number or not.
Java Program:
// Java program to Check Palindrome Number import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter a Number: "); int num = sc.nextInt(); int temp = num; int reverseNum = 0; while(temp > 0) { int digit = temp % 10; reverseNum = reverseNum * 10 + digit; temp = temp / 10; } if (num == reverseNum) { System.out.println(num + " is a Palindrome number"); } else { System.out.println(num + " is not a Palindrome number"); } sc.close(); } }
Output:
Enter a Number: 545 545 is a Palindrome number
- Java Program to Reverse a Number
- Java Program to Check Prime Number
- Java Program to Check Armstrong Number
- Java Program to Swap Two Numbers
- Java Program to Find Sum of Digits of a Number
Comments
Post a Comment