Palindrome number in Java

Advertisements

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

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements