Loading...
Posted by

What is a Palindrome Number?

A palindromic number or palindrome number is a number that remains the same when its digits are reversed. Like 16461.

Logic For Palindrome:

After understanding the meaning of palindrome number, its get easier for us to build a program to check whether a number is palindrome or not? So, as per the above definition we will take a number and than check if it remains same after getting reversed! so, the main logic behind this program is to take a number(input) and reverse it and than compare the reversed number with the number at the time of input. Lets see how to do it...
Source Code:
/* Palindrome Program In JAVA
  © Code Nirvana (www.codenirvana.in)
*/
import java.util.Scanner;
class Palindrome{ 
     public static void main(String args[]){ 
       System.out.print("Enter Number: ");
       Scanner read = new Scanner(System.in);
       int num = read.nextInt();
       int n = num;
       //reversing number
       int rev=0,rmd; 
       while(num > 0) 
       { 
         rmd = num % 10; 
         rev = rev * 10 + rmd; 
         num = num / 10; 
       } 
       if(rev == n) 
         System.out.println(n+" is a Palindrome Number!"); 
       else 
         System.out.println(n+" is not a Palindrome Number!"); 
     } 
}

Output:
Palindrome Output - Code Nirvana

Tutorial: Palindrome Program In Java



{ 16 comments... read them below or Comment }

  1. explain me in detail code for palindrome

    ReplyDelete
    Replies
    1. The above article explains about palindrome and its code in detail!
      What you want to know, or which part you didn't understand?

      Delete
  2. sir , please explain 12 and 16 line in detail .thankyou in advance

    ReplyDelete
    Replies
    1. Well before explaining I will suggest you to first run this program on paper (manually) for better understanding of the logic used!

      Here, rev is for reverse and rmd for remainder.
      In line 12 we created two variables rev and rmd of int type.
      and in line 16 we are assigning values to variable rev in every run. here's the explanation:
      rev = rev * 10 + rmd;
      suppose your no. is 12,
      In the first run rmd = 2 and rev = 0*10 + 2 == 2
      in second run rmd = 1 and rev = 2*10 + 1 == 21
      and in the third run our condition becomes false and loop breaks!
      Than the reverse of 12 is 21 , which is the required result....

      Hope you got our point, for more help feel free to contact us!

      Delete
  3. Thanks for the explanation! :)

    ReplyDelete
    Replies
    1. Our pleasure! Keep Visiting.....

      Delete
  4. i cant understand same example plzzzz help me

    ReplyDelete
    Replies
    1. please be specific about your problem, which example you didn't understand?

      Delete
  5. sir can you explain me the full logic of palindrome how it is done?

    ReplyDelete
    Replies
    1. Palindrome is just a number which remains the same on reversing it! So the logic is same as Reverse Number and further we just compared the reversed number with the input!
      Hope this helps to get the logic behind this!

      Delete
  6. sir, pls explain form 10 to 19 line

    ReplyDelete
    Replies
    1. Number reversing process is done there! nothing much to explain... check reverse number post @ our blog.

      Delete
  7. palindrome is means to check it can reverse, so we getting number after dat we createing input after i cant understand.. pls help me wat logic s going inside it to check palinderome or nor pls help me

    ReplyDelete
    Replies
    1. First input number, make a copy of that num variable to variable n, apply reverse number logic to n and store reversed number in variable rev, finally check whether rev == num.... if condition is true means palindrome otherwise not!
      Hope this gives you better understanding about the logic and program concept!

      Delete
  8. Thanks sir, i understand ... thanks vry much for instead if scanner why we cannot use DataInputStream obj= new DataInputStream(System.in);

    ReplyDelete
    Replies
    1. hi satish

      BufferedReader, is a character stream I/O class. Character streams provide a convenient way for input and output in terms of characters (Unicode). BufferedReader is mostly used for taking input from the console, System.in. It takes an InputStreamReader object as an argument.

      A Scanner can do all that a BufferedReader or a DataInputStream can do and at the same level of efficiency as well. However, in addition a Scanner can parse the underlying stream for primitive types and strings using regular expressions. It can also tokenize the underlying stream with the delimiter of your choice. It can also do forward scanning of the underlying stream disregarding the delimiter.

      Delete

Important - Make sure you tick the "Notify Me" box below the comment form to be notified of follow up comments and replies.

Search Site

Categories