Java Program to Check if a Number is Palindrome - The Coding Shala

Home >> Java Programs >> Palindrome Number

 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


Other Posts You May Like
Please leave a comment below if you like this post or found some errors, it will help me to improve my content.

Comments

Popular Posts from this Blog

Shell Script to find sum, product and average of given numbers - The Coding Shala

Shell Script to Create a Simple Calculator - The Coding Shala

Add two numbers in Scala - The Coding Shala

New Year Chaos Solution - The Coding Shala

Richest Customer Wealth LeetCode Solution - The Coding Shala