Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Beginners Palindrome Number Example in Java

 
 

Share on Google+Share on Google+

Palindrome Number Example in Java

Advertisement
In this section, you will learn about the palindrome number and how to determine any number is palindrome or not.

Palindrome Number Example in Java 

     

In this section, you will learn about the palindrome number and how to determine any number is palindrome or not. First of all we are going to read about the palindrome number.  This is the number that the actual number and after reversing this, in both cases the number is same that is called palindrome number otherwise not. Brief description below:

Description of program:

With the help of this program, we are going to determine whether the given number is palindrome or not.  To achieve the desired result, firstly we have to define a class named "Palindrome". After that we will ask the user to enter any integer type number and then we will reverse it. After reversing the number we will check whether the given number is palindrome or not. If the given number is larger, then it will display a message "Out of range!". 

Here is the code of this program

import java.io.*;

public class Palindrome  {
  public static void main(String [] args){
  try{
  BufferedReader object = new BufferedReader(
  new 
InputStreamReader(System.in));
  System.out.println("Enter number");
  int num= Integer.parseInt(object.readLine());
  int n = num;
  int rev=0;
  System.out.println("Number: ");
  System.out.println(" "+ num);
  for (int i=0; i<=num; i++){
  int r=num%10;
  num=num/10;
  rev=rev*10+r;
  i=0;
  }
  System.out.println("After reversing the number: "" ");
  System.out.println(" "+ rev);  
  if(n == rev){
  System.out.print("Number is palindrome!");
  }
  else{
  System.out.println("Number is not palindrome!");
  }
  }
  catch(Exception e){
  System.out.println("Out of range!");
  }
  }

Download this example

 

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 4, 2007

Related Tutorials

Discuss: Palindrome Number Example in Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:12
anjibabu
September 11, 2011
java

It's simple nice and write the program in better way it's good
krishan kant
October 11, 2011
comment

i m vry thankfull to you , u hv solve my problems
AILEEN
November 23, 2011
appreciation

it helps us a lot,,,thank you so much for the help.
vic
March 24, 2012


what's the role of "i=0" in the for loop ?
madhurendra kumar
April 14, 2012
java

what is Palindrome Number.
Sandhya
June 21, 2012
Palindrome

import java.util.Scanner; public class Palindrome { static String str2 = ""; static String comString; public static void main(String[] args) { Scanner s = new Scanner(System.in); String enteredString = s.next(); comString = enteredString; int stringLength = enteredString.length(); for (int i = stringLength - 1; i >= 0; i--) { char currentChar = enteredString.charAt(i); String str1 = Character.toString(currentChar); str2 = str2.concat(str1); } if (str2.equals(comString)) { System.out.println("Is Pallandrome"); } else { System.out.println("Not a Pallandrome"); } } }
Blaze
July 15, 2012
nice

nice code men. it helped me w/ my assignment about this. thanks to your code. i understand it now why its palindrome or vice versa. xD
palak
July 29, 2012
java

how to run java program in dos promt
mohan
September 6, 2012
what is polindrome application development in java

what is methods in java
DMCA.com