×

Java Programs

Java Practice

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

Core Java Example Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.