Log in

View Full Version : How to find a palindrome using java program


Mwenda20
Jun 28, 2011, 06:38 AM
Write a java program that uses a method called palindrome() to determine if number inputs by the user is palindrome or not.
The numbers inputs can be between 2 to 6 digits long. If the number is 1 or more than 6 digit long the program should be display error and allow a user to enter again. When I tried this program I wrote like this but is not complete.

import javax.swing.*;
import javax.swing.JOptionPane;

public class Test6{

public static void main(String[] args) {
//... Local variables
String Number1;

Number1 = JOptionPane.showInputDialog(null, "Enter Number.");

int num = Integer.parseInt(Number1);


//... calling
int rever = palindrome(num);

//... method
}
private static int palindrome(int num) {
int rev;
rev = num % 10;
num = num / 10;
rev = rev * 10 + num;
System.out.print(" "+ rev);
if(num==rev)
System.out.println("The number is palindrome");

else

System.out.println("The number is not palindrome");


return rev;

}

}

Thanks for help