Reverse and Add given number repeatedly to get a Palindrome number
Last Updated :
31 Oct, 2023
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 = 195
Output: 9339
Input: N = 265
Output: 45254
Input: N = 196
Output: 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++
// C++ Program to implement reverse and add function
#include <bits/stdc++.h>
using namespace std;
/* Iterative function to reverse digits of num*/
long long reverseDigits(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 (reverseDigits(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 = reverseDigits(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 Program
int main()
{
ReverseandAdd(195);
ReverseandAdd(265);
return 0;
}
Java
// Java Program to implement reverse and add function
public class ReverseAdd {
/* Iterative function to reverse digits of num*/
long reverseDigits(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 (reverseDigits(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 = reverseDigits(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);
}
}
Python3
# Python Program to implement reverse and add function
# Iterative function to reverse digits of num
def reverseDigits(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 not
def isPalindrome(num):
return (reverseDigits(num) == num)
# Reverse and Add Function
def ReverseandAdd(num):
rev_num = 0
while (num <= 4294967295):
# Reversing the digits of the number
rev_num = reverseDigits(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 Code
ReverseandAdd(195)
ReverseandAdd(265)
C#
// C# Program to implement reverse and add function
using System;
class GFG {
/* Iterative function to reverse digits of num*/
static long reverseDigits(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 (reverseDigits(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 = reverseDigits(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
JavaScript
<script>
// Javascript program to implement
// reverse and add function
// Iterative function to reverse digits of num
function reverseDigits(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(reverseDigits(num) == num);
}
// Reverse and Add Function
function ReverseandAdd(num)
{
let rev_num = 0;
while (num <= 4294967295)
{
// Reversing the digits of the number
rev_num = reverseDigits(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 code
ReverseandAdd(195);
ReverseandAdd(265);
// This code is contributed by rameshtravel07
</script>
PHP
<?php
// PHP Program to implement reverse and add function
/* Iterative function to reverse digits of num*/
function reverseDigits($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 (reverseDigits($num) == $num);
}
/* Reverse and Add Function */
function ReverseandAdd($num)
{
$rev_num = 0;
while ($num <= 4294967295)
{
// Reversing the digits of the number
$rev_num = reverseDigits($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 Code
ReverseandAdd(195);
ReverseandAdd(265);
// This code is contributed by chandan_jnu
?>
Time complexity: O(log N) for a given input
Auxiliary space: O(1) because constant variables have been used
References: https://app.assembla.com/spaces/AASU_Fall2008_ProgrammingTeam/wiki
This article is contributed by Rahul Agrawal.
Similar Reads
Smallest number to be subtracted to convert given number to a palindrome Given an integer N, the task is to find the smallest number to be subtracted from N to obtain a palindrome. Examples: Input: N = 1000Output: 1Explanation: Since 1000 â 1 = 999, which is a palindrome, the smallest number to be subtracted is 1. Input: N = 3456Output: 13Explanation: Since 3456 â 13 = 3
5 min read
Given a number, find the next smallest palindrome Given a number, in the form of an array num[] of size n containing digits from 1 to 9(inclusive). The task is to find the next smallest palindrome strictly larger than the given number.Examples:Input: num[] = [9, 4, 1, 8, 7, 9, 7, 8, 3, 2, 2]Output: [9, 4, 1, 8, 8, 0, 8, 8, 1, 4, 9]Explanation: Next
15+ min read
Count the number of digits of palindrome numbers in an array Given an array arr[] with N integers. The task is to count all the digits of all palindrome numbers present in the array.Examples: Input: arr[] = {121, 56, 434} Output: 6 Only 121 and 434 are palindromes and digitCount(121) + digitCount(434) = 3 + 3 = 6Input: arr[] = {56, 455, 546, 234} Output: 0 Ap
8 min read
Add number and its reverse represented by Linked List Given a number represented by a linked list, write a function that returns the sum of that number with its reverse form, and returns the resultant linked list. Note: Avoid modifying the original linked list. Examples: Input: 1->2->3->4 Output: 5->5->5->5Explanation: Input list: 1 2
12 min read
Minimum number of palindromes required to express N as a sum | Set 2 Given a number N, we have to find the minimum number of palindromes required to express N as a sum of them. Examples: Input : N = 11 Output : 1 Explanation: 11 is itself a palindrome.Input : N = 65 Output : 3 Explanation: 65 can be expressed as a sum of three palindromes (55, 9, 1).In the previous p
11 min read