Tools: VSCode | Source Code: GitHub
The Palindrome program is designed to check whether a number becomes a palindrome after adding it to its reverse and repeating the process two more times. The user inputs a series of numbers, and the program checks if each number satisfies this condition. First, it ensures the input number is at least three digits long. If valid, the number is added to an array and reversed for further calculations. The goal is to add the number to its reverse, repeat this for the resulting sum, and check if the final result is a palindrome.
The logic behind the palindrome check involves reversing the number by breaking it down into its digits, reassembling them in reverse order, and performing arithmetic operations on them. The program adds the original number to its reverse, reverses the sum, and repeats the process twice. By using a simple loop, it handles the reverse calculation, ensuring the operations are carried out efficiently. If the final number and its reverse are equal, the program prints "True," indicating a palindrome has been found. Otherwise, it prints "False."
While working on this project, I had to learn several important Java concepts. I learned how to create a reverse method for a number using loops, and how to calculate the total of digits in a number using the modulus and division operations. The `size()` method in Java helped me determine the number of elements in a list, and I learned to use the index of an array through `ArrayList.get()`. Adding input to a list in Java was done using the `add()` method, and I created input-based for loops to handle multiple user inputs. Additionally, I worked on finding the total sum of elements in a list using a loop that iterates over the indexes. These skills were crucial for building the logic behind this palindrome-checking program.
import java.util.ArrayList;
import java.util.Scanner;
public class Palindrome {
public static void main(String[] args) {
Scanner loopNumberInput = new Scanner(System.in);
System.out.println("How many numbers are you going to write:");
int numberTimes = loopNumberInput.nextInt();
ArrayList<Integer> numberPalListNormal = new ArrayList<>();
ArrayList<Integer> numberPalListReversed = new ArrayList<>();
for (int i = numberTimes; i > 0; i--) {
System.out.println("Write your number:");
String userInputStr = loopNumberInput.next();
if (userInputStr.length() >= 3) {
int userInput = Integer.parseInt(userInputStr);
int reverse = 0;
numberPalListNormal.add(userInput);
int copyInput = userInput;
while (copyInput != 0) {
int digit = copyInput % 10;
reverse = reverse * 10 + digit;
copyInput = copyInput / 10;
}
numberPalListReversed.add(reverse);
int sum = userInput + reverse;
int reverseOfSum = reverseNumber(sum);
int sumTwice = sum + reverseOfSum;
int reverseOfSumTwice = reverseNumber(sumTwice);
int finalSum = sumTwice + reverseOfSumTwice;
if (finalSum == reverseNumber(finalSum)) {
System.out.println("True");
} else {
System.out.println("False");
}
} else {
System.out.println("Your number should be at least 3 characters.");
i++;
}
}
loopNumberInput.close();
}
public static int reverseNumber(int number) {
int reverse = 0;
while (number != 0) {
int digit = number % 10;
reverse = reverse * 10 + digit;
number = number / 10;
}
return reverse;
}
}