Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 26-11-2009
Member
 
Join Date: Nov 2009
Posts: 57
how to write program on palindrome in java?

Hello,I am last year B.Sc.IT student.In my last exam in java paper one question has asked "write program on palindrome in java.". I tried various method but I unable to write that program.So I asked this question on this forum to get any idea about this program.Please if you know any logic behind this program then help me.
Thanks in advanced.
Reply With Quote
  #2  
Old 26-11-2009
Member
 
Join Date: May 2008
Posts: 2,008
Re: how to write program on palindrome in java?

palindrome program is used to check whether input number is palindrome or not. In following program I used Palindro class to check whether input number is palindrome or not.





Code:
import java.io.*;

public class Palindro  {
      public static void main(String [] args)
      {
      try 
        {
      BufferReader obj = new BufferReader(
                  new InputStreamReader(System.in));
      System.out.println("Enter the number");
      int p= Integer.parseInt(obj.readLine());
        int x = p;
        int set=0;
        System.out.println("Number: ");
        System.out.println(" "+ p);
        for (int t=0; i<=p; i++)
            {
            int q=p%10;
            p=p/10;
            set=set*10+q;
            t=0;
            }
        System.out.println("After reversing : "+ " ");
        System.out.println(" "+ set);      
        if(x == set){
        System.out.print("The Number you entered is palindrome!");
        }
        else{
        System.out.println("Number you entered is not palindrome!");
        }
      }
      catch(Exception e){
        System.out.println(" Number you entered is out of range!");
      }
  }
}

Last edited by Katty : 26-11-2009 at 04:58 PM.
Reply With Quote
  #3  
Old 26-11-2009
Member
 
Join Date: Apr 2008
Posts: 2,001
Re: how to write program on palindrome in java?

I think this is the most simple program on palindrome. Just try it.





Code:
 


      import java.util.Scanner;

      public class Palindrome {

      public static void main(String args[])
     {

      Scanner CS = new Scanner(System.in);
  
      String t="";
  
      System.out.println("Enter number of length 5");
 
      if(CS.hasNext())
 
      t =t+CS.next();
  
      if(t.length()!=5)
 
      System.out.println("you have entered wrong number again");
 
       
      if(t.charAt(0)==t.charAt(4) && t.charAt(1)==t.charAt(3))

      System.out.println(""+t+" is Palindrome");

      else

      System.out.println(t+" is not Palindrome");

       }

      }
Reply With Quote
  #4  
Old 26-11-2009
Member
 
Join Date: May 2008
Posts: 2,293
Re: how to write program on palindrome in java?

I had written following program on palindrome just try to compile in your computer.


Code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class MainClass
{
public static void main(String[] args) throws IOException
{
String wor;
BufferedReader opt=new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter a String or number :");

wor=opt.readLine();

int length = wor.length();

int left = 0;

int right = wor.length() -1; 

int ct=0;

while (left < right) 

{

if (wor.charAt(left) != word.charAt(right))

ct=0;

else

ct++;

left++; 

right--; // 

}

if(ct==0)

System.out.println(" Entered value is not Palindrome");

else

System.out.println("Entered value is Palindrome");

}

}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "how to write program on palindrome in java?"
Thread Thread Starter Forum Replies Last Post
How to write program to generate a pascal triangle in java? Aandaleeb Software Development 7 11-09-2011 12:37 PM
How to write java program to print pyramid of stars? Nadiaa Software Development 6 19-08-2011 05:03 PM
Need help to write this program in java? frkadeel Software Development 1 01-12-2010 03:58 PM
How to write string data to file using java program? Linoo Software Development 5 21-01-2010 09:26 PM
How to write java program to find factorial of number? Balamohan Software Development 5 28-11-2009 10:14 PM


All times are GMT +5.5. The time now is 12:27 PM.