PALINDROME IN JAVA

PALINDROME OF A NUMBER

A palindrome of a number is such that if we reverse it , it will not change . For example : number ‘12345321’ is apalindrome because its reverse also forms the same number . The number ‘12345’ is not a palindrome because its reverse ‘54321’ is not the same as ‘12345’ .
PROGRAM  FOR  PALINDROME  IN  JAVA
import java.util.Scanner;
public class Palindrome {

public static void main(String[] args) {
int t , n , r ,s =0;
System.out.println(“Enter any number : “);
Scanner sc =new Scanner(System.in);
n =sc.nextInt();          //taking value from the user
if(n<0){
System.out.println(“Enter any positive numbers  “);

}
else {
t =n;                 //assigning the value of n to t
while(n>0){
r =n%10;          //getting remainder
n=n/10;           //shorting the value of n
s =(s*10)+r;      //forming the reverse of a number

}
if(t ==s){
System.out.println(“The number “+t+” is a palindrome “);

}
else System.out.println(“The number “+t+” is not a palindrome “);

}
}
}

Leave a Reply