Home » Java solved programs » Core java programs

Java program to check whether a given number is palindrome or not



In this java program, we are going to check whether a given number if palindrome or not? Here, we are taking an integer number and checking whether it is palindrome or not?
Submitted by Preeti Jain, on March 11, 2018

Given an integer number and we have to check whether it is palindrome or not using java program.

Check palindrome number in java

import java.util.Scanner;

class CheckNumberPalindromeOrNotPalindromeClass{
	public static void main(String[] args){
		//rev variable is used to store reverse of actual_num
		//actual_num contains number accepted from user
		//temp_num variable is used for temporary purpose due to inconsistency
		int rev = 0, rem,actual_num,temp_num;

		//create Scanner class object
		Scanner sc = new Scanner(System.in);

		//Display input for user
		System.out.println("Enter any number :");

		//Accept input from keyboard
		actual_num = sc.nextInt();
		temp_num = actual_num;

		while(temp_num > 0){
			//Store remainder
			rem = temp_num % 10;
			// Store number in reverse form
			rev = rev*10+rem;
			//Truncate last digit from number
			temp_num = temp_num/10;
		}

		//actual_num value and rev value matches it means 
		//number palindrome otherwise not palindrome
		if(rev == actual_num)
			System.out.println("Given number is palindrome");
		else
			System.out.println("Given number is not palindrome");
	}
}

Output

Run(1)
D:\Java Articles>java CheckNumberPalindromeOrNotPalindromeClass
Enter any number :
121
Given number is palindrome

Run(2)
D:\Java Articles>java CheckNumberPalindromeOrNotPalindromeClass
Enter any number :
234
Given number is not palindrome





Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.



Comments and Discussions





© https://www.includehelp.com (2015-2018), Some rights reserved.




close Like other websites, this site uses cookies to deliver relevant ads based on your interest, by using our website, you acknowledge that you have read our privacy policy.