Palindrome number in Java

Program for palindrome number in java

In the following question, we are supposed to ask the user for an Integer input and then check if it is a Palindrome Number.

Palindrome Number are those numbers which when reversed are identical to the original number.

Example: 2576752 when reversed will be 2576752, thus it is a Palindrome Number.

The standard algorithm will be:

  1. Enter and Store the Integer Input.
  2. Store the same input in a duplicate variable for later use.
  3. Use a While loop which will run until N is reduced to 0.
  4. Construct to extract each individual digit from the extreme right using modulus operator.
  5. Keep Accumulating it in a separate variable within the loop construct.
  6. After the loop terminates, finally compare if the resulting number is equivalent to the original number.
  7. If the result is true, it is a Palindrome Number.
  8. Print the result accordingly.

Java code:

/* Program to check if a number is Palindrome or not.
 * Palindrome Numbers are numbers which if reversed will result in the original number.
   Example: 11,121,1331,159951,4378734,etc. */

import java.util.*;

class Palindrome { public static void main() { Scanner inp = new Scanner(System.in); System.out.print("\n Enter Number: "); int n = inp.nextInt(); int a, s = 0, m = n;

	while (n != 0) // Extracting each digits and accumulating its sum.
	{
		a = n % 10;
		s = s * 10 + a;
		n = n / 10;
	}

	if (m == s) // Checking if reverse and original are identical.
		System.out.println(m + " is a Palindrome Number");
	else
		System.out.println(m + " is not a Palindrome Number");

}

}

Output:

Enter Number: 3625263
3625263 is a Palindrome Number

Enter Number: 4472414 4472414 is not a Palindrome Number

Palindrome number in Java