Checking whether a number is palindrome or not, is one of the earlist classic problem in programming. A number is said to be palindrome if on reversing the number, we get the same number. For example, consider the number 121, on reversing we get 121. So, it is a palindrome number. Java program to check whether a number is palindrome or not is given.
package MathUtility;
class PalinNumber
{
boolean palincheck(int num)
{
int rem, div, rev=0, back;
back = num;
while(num>0)
{
rem = num %10;
rev = rev*10+rem;
num = num /10;
}
if(back==rev)
return true;
else
return false;
}
public static void main(String args[])
{
if(args.length==0)
{
System.out.println("No input provided");
}
else
{
int num = Integer.parseInt(args[0]);
PalinNumber pn = new PalinNumber();
if(pn.palincheck(num))
System.out.println(num + " is palindrome.");
else
System.out.println(num + " is not palindrome.");
}
}
}
To compile the program
javac -d . PalinNumber.javaAnd to run the program
java MathUtility.PalinNumber 12321
No comments:
Post a Comment