Here is a java program to check whether a number is palindrome or not using loop. An integer is called palindrome number, If the number remains same after reversing the sequence of it's digits. To check whether a number is palindrome number or not, we will create a copy of input number and reverse it's digits. If reversed number is same as input number then it is a palindrome number other not a palindrome number.
Algorithm to check whether a number is palindrome or not
- Take a number as input from user and store it in an integer variable(Let's call it num).
- Reverse the digits of num, and store it in another integer variable(Let's call it reverse).
- Compare num and reverse.
- If both are equal then num is palindrome number otherwise not a palindrome number.
Java program to check for palindrome numbers
package com.tcc.java.programs; import java.util.*; public class PalindromeNumberCheck { public static void main(String args[]) { int num, temp, reverse = 0, rightDigit; Scanner in = new Scanner(System.in); System.out.println("Enter a number"); num = in.nextInt(); temp = num; /*reverse num and store it in reverse */ while(temp != 0){ rightDigit = temp % 10; reverse = reverse*10 + rightDigit; temp = temp/10; } if(reverse == num) System.out.print(num + " is Palindrome number"); } else { System.out.print(num + " is not a Palindrome number"); } } }Output
Enter a number 1234321 1234321 is Palindrome number
Enter a number 72537625 72537625 is not a Palindrome number
Recommended Posts