Reverse and Add Function - GeeksforGeeks
The reverse and add function starts with a number, reverses its digits, and adds the reverse to the original. If the sum is not a palindrome, repeat this procedure until it does.
Write a program that takes number and gives the resulting palindrome (if one exists). If it took more than 1,000 iterations (additions) or yield a palindrome that is greater than 4,294,967,295, assume that no palindrome exist for the given number.
Read full article from Reverse and Add Function - GeeksforGeeks
The reverse and add function starts with a number, reverses its digits, and adds the reverse to the original. If the sum is not a palindrome, repeat this procedure until it does.
Write a program that takes number and gives the resulting palindrome (if one exists). If it took more than 1,000 iterations (additions) or yield a palindrome that is greater than 4,294,967,295, assume that no palindrome exist for the given number.
/* Iterative function to reverse digits of num*/ long reversDigits(long num) { long rev_num = 0; while (num > 0) { rev_num = rev_num*10 + num%10; num = num/10; } return rev_num; } /* Function to check whether he number is palindrome or not */ boolean isPalindrome(long num) { return (reversDigits(num) == num); } /* Reverse and Add Function */ void ReverseandAdd(long num) { long rev_num = 0; while (num <= 4294967295l) { // Reversing the digits of the number rev_num = reversDigits(num); // Adding the reversed number with the original num = num + rev_num; // Checking whether the number is palindrome or not if(isPalindrome(num)) { System.out.println(num); break; } else if (num > 4294967295l) { System.out.println("No palindrome exist"); } } }