|
| |||||||||
| Tags: arrays, java, loop, palindrome, string |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| 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
| |||
| |||
| 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
| ||||
| ||||
| 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
| ||||
| ||||
| 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");
}
} |
![]() |
|
| Thread Tools | Search this Thread |
| |
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 01:37 PM |
| How to write java program to print pyramid of stars? | Nadiaa | Software Development | 6 | 19-08-2011 06: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 |