The palindrome number program is one of the most asked questions in the technical round during placement. Though the program is easy to write and understand many new students in the programming find it difficult to understand. In this article on the palindrome number, we will try to simplify the program and algorithm for you.
Table of Contents
What is a Palindrome or Palindromic number?
A given number that stays the same when its digits are reversed is called a palindrome. It also applies to the strings.
for example,
121 <-> 121
3663 <-> 3663
Above are the palindromic number.
Now we are going to understand the palindrome number in the form of a flowchart so your concept about it will get clear.
Flowchart to check if a given number is Palindrome or not
Algorithm to check if a given number is Palindrome or not
Step 1: Start
Step 2: Read the input number from the user
Step 3: Declare and initialize the variable reverse and assign input to a temp variable tempNum=num
Step 4: Start the while loop until num !=0 becomes false
- rem = num % 10
- reverse*= 10 + rem
- num = num / 10
Step 5 : Check if reverse == tempNum
Step 6: If it’s true then the number is a palindrome
Step 7: If not, the number is NOT a palindrome
Step 8: Stop
Also Read: Factorial of a given number – Algorithm, Flowchart, and Program
Palindrome number in Java
import java.util.Scanner;
class Palindrome {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter Number: ");
int num = sc.nextInt();
int reverse = 0;
int tempNum = num;
while(num>0){
int remainder = num % 10;
reverse = reverse*10 + remainder;
num = num / 10;
}
if(reverse == tempNum) {
System.out.println("Given number is Palindrome");
} else {
System.out.println("Given number is not Palindrome");
}
}
}
In this way you can check if given number is palindrome or not.