Palindrome Number:-
A palindrome number a number which remains same after reversing it’s digits.
Example – 16761, this is a palindrome number because after reversing its digit its value remains the same. but 123 is not a palindrome number because after reversing its digit its value not remains the same.
Steps to Check if a Number is Palindrome or Not
- Take Input from the user using a scanner.
- Reverse the number and store it to a new variable.
- check reverse number and the number entered by user is same or not.
- if both are equal then the number is palindrome.
- else number is not palindrome.
Palindrome number in java:-
import java.util.Scanner;
public class PalindromeNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");//Taking input from user
int n=sc.nextInt();
int temp=n;
int reverse=0;
//loop for reversing the number.
while(temp>0) {
reverse=reverse*10+temp%10;
temp=temp/10;
}
//checking the number is palindrome or not.
if(n==reverse) {
System.out.println("No is palindrome");
}
else {
System.out.println("No is not palindrome");
}
}
}
Similar Java Tutorials
- Odd Even in Java
- Fibonacci Series Java
- Switch statements in Java
- For loop in Java
- While loop in Java
- Method signature in Java
- Method overloading in Java
- Has A Relationship
- Inheritance in Java
- Encapsulation in Java
- Abstraction in Java
- try with multiple catch block in Java
- Threads in Java
- Thread priority in Java