###
**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:**

**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:**

####
**Tutorial: Palindrome Program In Java**

explain me in detail code for palindrome

ReplyDeleteThe above article explains about palindrome and its code in detail!

DeleteWhat you want to know, or which part you didn't understand?

Following comparison is not happening when a palindrome number is given e.g. 454

Deleteif(rev == n)

System.out.println(n+" is a Palindrome Number!");

reversed number is not equaling to the number inputted ..please let us know

@Srinath I tested the above code again and its working fine!

Deletecheck here: ideone.com/H7WSmw

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

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

DeleteHere, 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!

can u tell me how in d second run rev=1???

DeleteThanks for the explanation! :)

ReplyDeleteOur pleasure! Keep Visiting.....

DeleteSir,

DeleteHow do i check weather a String is palindrome or not?

@ZeeShan Checking string palindrome is very easy using StringBuilder!

Deletecheck the below function for checking whether string is palindrome of not:

public static boolean isPalindrome(String S) {String rev = new StringBuilder(S).reverse().toString();

if( S.equals( rev ) ) return true;

return false;

}

i cant understand same example plzzzz help me

ReplyDeleteplease be specific about your problem, which example you didn't understand?

Deletesir can you explain me the full logic of palindrome how it is done?

ReplyDeletePalindrome 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!

DeleteHope this helps to get the logic behind this!

sir, pls explain form 10 to 19 line

ReplyDeleteNumber reversing process is done there! nothing much to explain... check reverse number post @ our blog.

Deletepalindrome 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

ReplyDeleteFirst 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!

DeleteHope this gives you better understanding about the logic and program concept!

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

ReplyDeletehi satish

DeleteBufferedReader, 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.