Palindrome of a integer
1.) Read a number. Reverse the digits are store them. See of the original number and its reverse are palindrome or equal.
2.) Find first and last numbers and adding them together
3.) Find all even integers in the number and adding them together
4.) Find all odd integers in the number and adding them together
public class Palindrome {
public static void main(String[] args) {
System.out.println();
isPalindrome(-1221);
isPalindrome(-9876);
isPalindrome(7007);
isPalindrome(18081);
isPalindrome(18021);
isPalindrome(-12341);
isPalindrome(24565);
}
public static void isPalindrome(int num) {
int original_num = num;
System.out.printf("Orignal number is " + original_num);
// this is the formula to return the number of digits in a
int length = (int) (Math.log10(Math.abs(num)) + 1);
int x = 1;
int result;
int rev_num = 0;
int first_num = 0;
int last_num = 0;
while (x < length + 1) {
result = num % 10;
if (x == 1) {
first_num = result;
}
if (x == length)
last_num = result;
rev_num = rev_num + (result * (int) (Math.pow(10, length - x)));
num = num / 10;
x++;
}
System.out.printf(" & reverse number is " + rev_num);
if (original_num == rev_num) {
System.out.println(" and the numbers are palindrome");
System.out.println("First number " + first_num + " + " + "Last number " + last_num + " = " + (first_num + last_num));
System.out.println();
} else {
System.out.println(" and the numbers are not palindrome");
System.out.println("First number " + first_num + " + " + "Second number " + last_num + " = " + (first_num + last_num));
System.out.println();
}
}
}
C:\Users\Win10x64i7\.jdks\openjdk-23.0.1\bin\java.exe "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2024.2.4\lib\idea_rt.jar=57130:D:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2024.2.4\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath C:\Users\Win10x64i7\IdeaProjects\Palidrome\out\production\Palindrome Palindrome
Orignal number is -1221 & reverse number is -1221 and the numbers are palindrome
First number -1 + Last number -1 = -2
Sum even numbers= -4
Sum odd numbers= -2
Orignal number is -9876 & reverse number is -6789 and the numbers are not palindrome
First number -6 + Second number -9 = -15
Sum even numbers= -14
Sum odd numbers= -16
Orignal number is 7007 & reverse number is 7007 and the numbers are palindrome
First number 7 + Last number 7 = 14
Sum even numbers= 0
Sum odd numbers= 14
Orignal number is 18081 & reverse number is 18081 and the numbers are palindrome
First number 1 + Last number 1 = 2
Sum even numbers= 16
Sum odd numbers= 2
Orignal number is 18021 & reverse number is 12081 and the numbers are not palindrome
First number 1 + Second number 1 = 2
Sum even numbers= 10
Sum odd numbers= 2
Orignal number is -12341 & reverse number is -14321 and the numbers are not palindrome
First number -1 + Second number -1 = -2
Sum even numbers= -6
Sum odd numbers= -5
Orignal number is 24565 & reverse number is 56542 and the numbers are not palindrome
First number 5 + Second number 2 = 7
Sum even numbers= 12
Sum odd numbers= 10
Process finished with exit code 0