Integer Palindrome in java: Algorithm and programs

What is an Integer palindrome in Java?

Integer palindrome in java is the Integer or the string is the same after reversing it.

for example:-

  1. we take a number 141 the palindrome of this number is 141, so it is a palindrome number.
  2. next, we take number 456 the palindrome of this number is 654, so it is not a palindrome number.
  3. now we take a string “baab” the palindrome of this string is “baab”, so it is a palindrome string.
  4. now we take a string “gettingplaced” the palindrome of this string is “decalpgnitteg”, so it is not a palindrome string.

Algorithm

START
Step 1 → Take an integer a,temp,sum
Step 2 → initilized temp with a
Step 3 → using while loop where n>0
Step 4 → take an integer r
Step 5 → initilized r with n%10;
Step 6 → initilized sum with (sum*10)+r
Step 7 → initilized n with n/10
Step 8 → if temp==sum then it is an integer palindrome
Step 9 → else it is not an integer palindrome
STOP

The integer is a palindrome in java using a while loop

class Palindrome{

public static void main(String args[]){

int a,s=0,temp;

int n=141;

temp=n;

while(n>0){

a=n%10;  //getting remainder

s=(s*10)+a;

n=n/10;

}

if(temp==s)

System.out.println(“it is an palindrome “);

else

System.out.println(“not palindrome”);

}

}

Output

it is an palindrome

The integer is a palindrome in java using a for loop

 

class Palindrome
{
public static void main(String arg[])
{
    int n,temp,s,r;
    n=141;
    temp=n;
    for(s=0;n>0;n/=10)
    {
        r=n%10;
        s=(s*10)+r;
    }
   if(s==temp)
        System.out.println(temp+” is a palindrome number “);
   else
         System.out.println(temp+” is not a palindrome number “);
                  
    }
}

Output

141   it is an palindrome

 

The integer is a palindrome in java using a scanner

class Palindrome
{
public static void main(String arg[])
{
    int n,temp,s,r;
    Scanner sc=new Scanner(System.in);

    System.out.println(“Enter any number “);
    n=sc.nextInt();
    temp=n;
    for(s=0;n>0;n/=10)
    {
        r=n%10;
        s=(s*10)+r;
    }
   if(s==temp)
        System.out.println(temp+” is a palindrome number “);
   else
         System.out.println(temp+” is not a palindrome number “);
                  
    }
}

Output

Enter any number 141

141   it is an palindrome

 

Click here for more questions in java

String Palindrome in java: Algorithm and programs

Leave a Comment