Results 1 to 4 of 4

Thread: how to write program on palindrome in java?

  1. #1
    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.

  2. #2
    Join Date
    May 2008
    Posts
    2,012

    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.

  3. #3
    Join Date
    Apr 2008
    Posts
    2,005

    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");
    
           }
    
          }

  4. #4
    Join Date
    May 2008
    Posts
    2,297

    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");
    
    }
    
    }

Similar Threads

  1. How to write program to generate a pascal triangle in java?
    By Aandaleeb in forum Software Development
    Replies: 7
    Last Post: 11-09-2011, 12:37 PM
  2. How to write java program to print pyramid of stars?
    By Nadiaa in forum Software Development
    Replies: 6
    Last Post: 19-08-2011, 05:03 PM
  3. Need help to write this program in java?
    By frkadeel in forum Software Development
    Replies: 1
    Last Post: 01-12-2010, 03:58 PM
  4. How to write string data to file using java program?
    By Linoo in forum Software Development
    Replies: 5
    Last Post: 21-01-2010, 09:26 PM
  5. How to write java program to find factorial of number?
    By Balamohan in forum Software Development
    Replies: 5
    Last Post: 28-11-2009, 10:14 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,662,527.31426 seconds with 17 queries