Find palindrome number program in java using for loop

Find palindrome number program in java using for loop

The word “palindrome” has a Greek origin, a word, number, or set of characters that reads the same forward and backwards. Some examples of a palindrome are MALAYALAM, 343, 2001002, etc. Palindrome in Java can be implemented using many ways. Here we will discuss some important ways: 

  • Using while loops
  • Using for loops
  • Using recursion

What is a Palindrome Number?

Palindrome no. is the number that remains the same when its digits get reversed.

Ex: 15451, for example: If we take 131 and reverse it then after reversing the number remains the same.

Steps to Palindrome Number program

  • Input the number from the user.
  • Then Reverse it.
  • Compare the number with the number entered by the user.
  • If both the no.’s are the same then print the number as a palindrome
  • Else print not a palindrome.

Example 1:

Finding Palindrome number Using while loop

<strong>Method 1:</strong> Check given user input number is Palindrome or not in Java

import java.util.Scanner;
class expalindrome
{
public static void main(String args[])
{
int x,number, y=0,temp=0;
Scanner s=new Scanner(System.in);
System.out.println(“Enter any number: “);
number=s.nextInt();
x=number;
while(number>0)
{
x=number%10;
number=number/10;
temp=temp*10+x;
}
if(temp==y)
{
System.out.println(“Number is Palindrome”);
}
else
{
System.out.println(“not Palindrome”);
}
}
}

Output:

Enter any Number :

161

Number is Palindrome

Example 2:

Finding Palindrome number Using for loop

1) The number is called a palindrome if a number is equal to reverse of its number.

2) For loop repeats rem=num%10; s=(s*10)+rem these steps until num>0. If the condition is false, then it compares the s value with t, if both are equal then it prints the given number is a palindrome.

<strong>Method 2:</strong> Program to display given user input is Palindrome or not
class Palindrome
{
	public static void main(String arg[])	
	{
	    int num,t,s,rem;
             	    Scanner sc=new Scanner(System.in);
	    System.out.println("Enter any number ");
                   num=sc.nextInt();
	    t=num;
	    for(s=0;num>0;num/=10)
	    {
	    rem=num%10;
	    s=(s*10)+rem;
	    }
	   if(s==t)
		System.out.println(t+" is a palindrome number ");
                  else
		System.out.println(t+" is not a palindrome number ");
                  
	}
}

Output:

Enter any number

161

161 is a palindrome number

Enter any number

1231

1231 is not a palindrome number

Example 3:

Finding Palindrome number Using Using Recursion

1) Using the “Palin” object p, we will call the palindromeOrNot(a) method.

2) The method palindromeOrNot(int num) calls itself as palindromeOrNot(num) until num!=0, if num=0 then it returns sum and sum assigned to s and compares with t, if both are equal then it prints message as “palindrome number”.

<strong>Method 3:</strong> Program to display given user input is Palindrome or not
class Palin
{
	int sum=0,r;
	int palindromeOrNot(int num)
	{
	    if(num!=0)
	    {
	    r=num%10;
	    sum=(sum*10)+r;
	    num/=10;	
	    palindromeOrNot(num); 
	   }
	return sum;
	}
	public static void main(String arg[])	
	{
	    int a,t,s;
	    Palin p=new Palin();
             	    Scanner sc=new Scanner(System.in);
	    System.out.print("Enter a number :");
                   a=sc.nextInt();
	    t=a;
	    s=p.palindromeOrNot(a);
	    if(s==t)
		System.out.println("Palindrome number ");
                   else
		System.out.println("Not a Palindrome number ");
                  
	}
 
}

Output:

Enter a number :123454321
Palindrome number

Find palindrome number program in java using for loop – Find palindrome number program in java using for loop – Find palindrome number program in java using for loop – Find palindrome number program in java using for loop

Find palindrome number program in java using for loop