Reverse and Add given number repeatedly to get a Palindrome number

07 Mar, 2022
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.
Examples: 
Input: N = 195Output: 9339
Input: N = 265Output: 45254
Input: N = 196Output: No palindrome exist
Approach: Create a reverse and add function to start 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.
C++
Java
Python3
C#
PHP
Javascript
// C++ Program to implement reverse and add function#include <bits/stdc++.h>using namespace std;  /* Iterative function to reverse digits of num*/long long reversDigits(long long num){    long long rev_num = 0;    while (num > 0) {        rev_num = rev_num * 10 + num % 10;        num = num / 10;    }    return rev_num;}  /* Function to check whether the number is palindrome or not */bool isPalindrome(long long num){    return (reversDigits(num) == num);}  /* Reverse and Add Function */void ReverseandAdd(long long num){    long long rev_num = 0;    while (num <= 4294967295) {        // 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)) {            printf("%lld\n", num);            break;        }        else if (num > 4294967295) {            printf("No palindrome exist");        }    }}  // Driver Programint main(){    ReverseandAdd(195);    ReverseandAdd(265);    return 0;}
// Java Program to implement reverse and add functionpublic class ReverseAdd {    /* 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");            }        }    }      // Main method    public static void main(String[] args)    {        ReverseAdd ob = new ReverseAdd();        ob.ReverseandAdd(195l);        ob.ReverseandAdd(265l);    }}
# Python Program to implement reverse and add function  # Iterative function to reverse digits of numdef reversDigits(num):    rev_num = 0    while (num > 0):        rev_num = rev_num * 10 + num % 10        num = num//10    return rev_num  # Function to check whether # the number is palindrome or notdef isPalindrome(num):    return (reversDigits(num) == num)  # Reverse and Add Functiondef ReverseandAdd(num):    rev_num = 0    while (num <= 4294967295):        # 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)):            print (num)            break        else:            if (num > 4294967295):                print ("No palindrome exist")  # Driver CodeReverseandAdd(195)ReverseandAdd(265)
// C# Program to implement reverse and add functionusing System;  class GFG {    /* Iterative function to reverse digits of num*/    static 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 */    static bool isPalindrome(long num)    {        return (reversDigits(num) == num);    }      /* Reverse and Add Function */    static void ReverseandAdd(long num)    {        long rev_num = 0;        while (num <= 4294967295) {            // 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)) {                Console.WriteLine(num);                break;            }            else if (num > 4294967295) {                Console.WriteLine("No palindrome exist");            }        }    }      // Driver code    public static void Main()    {        ReverseandAdd(195);        ReverseandAdd(265);    }}  // This code is contributed by chandan_jnu
<?php// PHP Program to implement reverse and add function  /* Iterative function to reverse digits of num*/function reversDigits($num){    $rev_num = 0;    while ($num > 0)    {        $rev_num = $rev_num * 10 + $num % 10;        $num = (int)($num / 10);    }    return $rev_num;}  /* Function to check whether he number   is palindrome or not */function isPalindrome($num){    return (reversDigits($num) == $num);}  /* Reverse and Add Function */function ReverseandAdd($num){     $rev_num = 0;    while ($num <= 4294967295)    {        // 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))        {            print($num . "\n");            break;        }        else if ($num > 4294967295)        {            print("No palindrome exist");        }    }}  // Driver CodeReverseandAdd(195);ReverseandAdd(265);  // This code is contributed by chandan_jnu?>
<script>  // Javascript program to implement// reverse and add function  // Iterative function to reverse digits of numfunction reversDigits(num){    let rev_num = 0;    while (num > 0)    {        rev_num = rev_num * 10 + num % 10;        num = parseInt(num / 10, 10);    }    return rev_num;}  // Function to check whether he number // is palindrome or not function isPalindrome(num){    return(reversDigits(num) == num);}  // Reverse and Add Function function ReverseandAdd(num){    let rev_num = 0;          while (num <= 4294967295)    {                  // 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))        {            document.write(num + "</br>");            break;        }        else if (num > 4294967295)        {            document.write("No palindrome exist" +                            "</br>");        }    }}  // Driver codeReverseandAdd(195);ReverseandAdd(265);  // This code is contributed by rameshtravel07    </script>
9339
45254

References: https://app.assembla.com/spaces/AASU_Fall2008_ProgrammingTeam/wiki
This article is contributed by Rahul Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
Chandan_Kumar
rameshtravel07
amartyaghoshgfg
Reverse
Mathematical
Mathematical
Reverse
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.