Palindrome number in Java


Prev Tutorial Next Tutorial

Palindrome Number Program in Java

A Palindrome number is a number that remains the same when its digits are reversed. Like 16461, for example: we take 121 and reverse it, after revers it is same as original number.

Steps to write program

  • Get the number from user.
  • Reverse it.
  • Compare it with the number entered by the user.
  • If both are same then print palindrome
  • Else print not a palindrome.

Palindrome Number Program in Java

import java.util.Scanner;
class Palindrome 
{
public static void main(String[] args) 
{
int a,no,b,temp=0;
Scanner s=new Scanner(System.in);
System.out.println("Enter any num: ");
no=s.nextInt();
b=no;
while(no>0)
{
a=no%10;
no=no/10;
temp=temp*10+a;
}
if(temp==b)
{
System.out.println("Palindrome");
}
else
{
System.out.println("not Palindrome");
}
}
}

Output

Enter any num :
151
Palindrome

Syntax to compile and run java program

Syntax

for compile -> c:/>javac Palindrome.java
for run -> c:/>java Palindrome

Explanation of Code

  • Scanner s=new Scanner(System.in): are used for receive input from keyboard.
  • nextInt(): method are used for get integer type value from keyboard.
  • System.out.println("....."): are used for display message on screen or console.

Prev Tutorial Next Tutorial

Advertisements




Download Projects