I want to "write program on palindrome in java. I tried various methods but I unable to write that program.
Any idea
I want to "write program on palindrome in java. I tried various methods but I unable to write that program.
Any idea
1) Hi! I hope that my solution for your problem will help you. Write me back if you are satisfied with the 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!");
}
}
}
2) I hope this is easiest code to find palindrome of length five & help you a lot.
import java.util.Scanner;
public class Palindrom
{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
String s="";
System.out.println("Enter no <strong class="highlight">of</strong> length 5");
if(sc.hasNext())
s =s+sc.next();
if(s.length()!=5)
System.out.println("Wrong Number Enter again");
if(s.charAt(0)==s.charAt(4) && s.charAt(1)==s.charAt(3))
System.out.println(""+s+" Is Palindrom");
else
System.out.println(s+" Is not Palindrom");
}
}
Bookmarks