Palindrome number program in Java

In this section you will learn how to make palindrome number program in java. We will use while loop condition to perform palindrome number check operation.

What is palindrome number?

A palindrome number is a number such that if you reverse it, it will not change. The number should be remain same. To check whether a number is palindrome or not first we reverse it and then compare the number obtained with the original, if both are same then number is palindrome otherwise not.

Example of palindrome number:

212, 121, 77077, 11611.

What is scanner function?

Scanner function is use to take the input from user in java.

How program will work?

Program of palindrome number in Java


	import java.util.Scanner;

	class Palindrome
	{
		public static void main(String arg[])
		{
			int n, reverse=0, temp;
			Scanner sc = new Scanner(System.in);
			System.out.println("ENTER A NUMBER:- ");
			n = sc.nextInt();
			temp = n;
			while (temp != 0)
			{
			reverse = reverse * 10;
			reverse = reverse + temp % 10;
			temp = temp / 10;
			}
			if (n == reverse)
			{
				System.out.println("ENTER NUMBER IS PALINDROME.");
			}
			else
			{
				System.out.println("ENTER NUMBER IS NOT PALINDROME.");
			}
		}
	}

Output:


	ENTER A NUMBER:- 121
	ENTER NUMBER IS PALINDROME.

Advertisment

Post Your Comment