Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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.

 

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

 

                         

» View all related tutorials
Related Tags: c file class orm constructor object path struct const value name return instance paths create if send match to bot

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

9 comments so far (
post your own) View All Comments Latest 10 Comments:

for the java palindrome example shown in the yellow box at the top, how do you make it so, once it reverses the number once, you add the reversed number with its original number, and if that sum is still not a palindrome, how do you reverse that sum again?

Posted by heyhey on Friday, 05.30.08 @ 01:59am | #61377

i am very thankful to you because you give me a necessary program for examination

Posted by arun ranawat on Sunday, 03.23.08 @ 22:37pm | #53969

that my code....pleese i need help in my while and do....

import apcslib.*;
import chn.util.*;

public class Palindrome
{
String sentence;
String reverse;

public Palindrome ()
{
sentence ="";
reverse ="";
}
public void start()
{
getInput ();
reverseInput ();
compareSens ();
}
public void getInput()
{
System.out.println("Welcome To PALINDROME");
ConsoleIO console = new ConsoleIO();
System.out.print("Please enter your sentence ");
sentence = console.readLine();
sentence = sentence.toLowerCase();
System.out.println("Your sentence is " + sentence);
}

public void reverseInput ()
{
String temp = sentence.trim();
System.out.println("Your trim sentence is "+ temp);
int senLength=temp.length();
System.out.println("Length of the sentence is "+ senLength);
int n = temp.indexOf(" ");
System.out.println("The first white space is at "+n);
while (senLength>0)
{
temp=temp=temp.substring(0,n);
temp=temp.substring(n+1,temp.length());
System.out.println("The second white space is at " + temp);
reverse=reverse+sentence.charAt(senLength-1);
senLength--;
}
System.out.println("The reverse is " + reverse);
}
public void compareSens ()
{
if (sentence.equals(reverse))
System.out.println("The sentence is a palindrome");
else
System.out.println("The sentence is not a palindrome");
}
}

Posted by saima on Thursday, 02.21.08 @ 06:52am | #49269

i need help of my prg in palindrome
class Palindrome
{
public static void main(String[] args) throws IOException
{
String str[]=new String[1];
String dest[]=new String[1];
int j=0;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the String to Palindrome");
int i=str.length;
for(i=(i-1);i>=0;i--)
{
str[i]=br.readLine();
dest[j]=str[i];
j++;
}
//if(dest.compareTo(str)==0)
if(str.equals(dest))
{
System.out.println("String are Palindroome");
}
else
{
System.out.println("not palindrome");
}
}
}

plz correct the program
thanks in adv

Posted by Siva on Tuesday, 02.12.08 @ 19:23pm | #48041

your for-loop is astonishing...
do you realize what are you doing there?

Posted by afasias on Thursday, 01.17.08 @ 14:48pm | #45431

Why we are using Integer.parseInt()method

Posted by Rashmi on Tuesday, 11.6.07 @ 11:09am | #35869

Would it not be easier to store the line input into a string, then parsing the string into an integer, then parsing the reversed string into a separate integer variable and comparing them?

Posted by Miguel on Monday, 10.8.07 @ 10:13am | #32437

Java Program for palindrome

Posted by Dhanush on Sunday, 08.5.07 @ 14:51pm | #22705

I tried to run this code but it won't work on zeros.. it converts the zeros to one....

Posted by ~javaness~ on Tuesday, 05.8.07 @ 08:13am | #15435

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.