In this tutorial you will learn about the Program to check Palindrome Number In Java and its application with practical example.
In this tutorial, we will learn to create a Java Program to Check Palindrome Number In Java using Java programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following Java programming topics:
- Java Operators.
- Basic Input and Output function in Java.
- Class and Object in Java.
- Basic Java programming.
- If-else statements in Java.
- For loop in Java.
What is Palindrome Number.
A number is called a Palindrome Number if number is same after reverse the number it self.
121 <=> 121
323 <=> 323
656 <=>656.
Algorithm to find Palindrome Number
- Get the number from user to check condition for palindrome
- pass the number in temporary variable.
- Reverse the number.
- Check Temporary number with reversed number.
- If both numbers are same number is “Palindrome number”.
- Else not palindrome.
Program to check Palindrome Number In Java
In this program we would find given number is a Palindrome number or not .first of we would take a value from user and find the Palindrome number.let have a look at the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
// java preogram to find given number is a palindrome or not import java.util.*; class Palindrome { public static void main(String args[]) { // declaring variables and takin values from user int r,sum=0,temp; int number; Scanner sc = new Scanner(System.in); System.out.println("Enter any number "); number = sc.nextInt(); // copying original to temp variables.. temp=number; // loop run till value of number greater than 0 while(number>0) { r=number%10; sum=(sum*10)+r; // reversing number number=number/10; } // compare if original number equals to reverse number or not.. if(temp==sum) System.out.println("palindrome number "+ temp); else System.out.println("not palindrome" + temp); } } |
Output
Palindrome number
Not a palindrome Number.
In the above program, we have first declared and initialized a set variables required in the program.
- number= it will hold entered number.
- sum= it will hold sum of factors.
After that we take a number from user and find given number is a Palindrome number or not.
Now we will run the loop until (number>0)
- Here we used a while loop to reverse number and store the reversed number in sum.
- Then we will check entered number is equal to the reverse number.
- If Number is equal to reverse number (number==sum) then number is palindrome.
If not number is not a Palindrome.