/*
pallindrome = 121 = 121 print same as reverse
Number will be palindrome if it is equal to its reverse */
import java.util.*;
import java.lang.*;
import java.io.*;
// The main method must be in a class named "Main".
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int originalNumber = n ;
int rev = 0 ;
boolean isNumberNegative = false ;
if(n < 0){
isNumberNegative = true ;
n = n* -1 ;
}
while(n>0){
int last = n % 10;
rev = rev * 10 + last ;
n = n/10 ;
}
if(isNumberNegative){
rev = rev * -1 ;
}
boolean isPalindrome = ( rev == originalNumber);
System.out.println("Is palindrome : " + isPalindrome);
}
}
/*
The given code is checking whether a number is a palindrome or not. Here's an explanation of how it works:
1. The program takes an input number n from the user.
2. It assigns the value of n to the variable originalNumber to store the original number.
3. It initializes the variable rev to 0, which will be used to store the reverse of the number.
4. It sets the boolean variable isNumberNegative to false.
5. If the input number n is negative, it sets isNumberNegative to true and makes n positive by multiplying it by -1. This step is done to handle negative numbers appropriately during the palindrome check.
6. The program enters a while loop that continues until n becomes 0.
7. Inside the loop, it calculates the last digit of n using the modulus operator % and assigns it to the variable last.
8. It updates the rev variable by multiplying it by 10 and adding the value of last, effectively reversing the number digit by digit.
9. It divides n by 10 to remove the last digit and move on to the next digit.
10. Once the loop ends, the program checks if the isNumberNegative flag is true. If so, it multiplies the rev variable by -1 to make it negative, so the reversed number matches the original negative number.
11. Finally, it checks if the reversed number (rev) is equal to the original number (originalNumber) and assigns the result to the boolean variable isPalindrome. */
12. The program prints the result of whether the number is a palindrome or not.
In summary, the code reverses the input number and compares it with the original number to determine if it is a palindrome. The handling of negative numbers ensures that the check is done correctly for negative palindromic numbers as well.
To embed this project on your website, copy the following code and paste it into your website's HTML: