We care about your data privacy. HackerEarth uses the information that you provide to contact you about relevant content, products, and services.
Our Privacy Policy and Terms of Service will help you understand that you are in control of your data at HackerEarth.
View Discussion
Improve Article
Save Article
ReadDiscussView Discussion
Improve Article
Save Article
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++
#include <bits/stdc++.h>
using
namespace
std;
long
long
reverse Digits(
long
long
num)
{
long
long
rev_num = 0;
while
(num > 0) {
rev_num = rev_num * 10 + num % 10;
num = num / 10;
}
return
rev_num;
}
bool
isPalindrome(
long
long
num)
{
return
(reverse Digits(num) == num);
}
void
ReverseandAdd(
long
long
num)
{
long
long
rev_num = 0;
while
(num <= 4294967295) {
rev_num = reverse Digits(num);
num
= num + rev_num;
if
(isPalindrome(num)) {
printf
(
"%lld\n"
, num);
break
;
}
else
if
(num > 4294967295) {
printf
(
"No palindrome exist"
);
}
}
}
int
main()
{
ReverseandAdd(195);
ReverseandAdd(265);
return
0;
}
Java
public
class
ReverseAdd {
long
revers eDigits(
long
num)
{
long
rev_num =
0
;
while
(num >
0
) {
rev_num = rev_num *
10
+ num %
10
;
num = num /
10
;
}
return
rev_num;
}
boolean
isPalindrome(
long
num)
{
return
(reversDigits(num) == num);
}
void
ReverseandAdd(
long
num)
{
long
rev_num =
0
;
while
(num <= 4294967295l) {
rev_num = reversDigits(num);
num
= num + rev_num;
if
(isPalindrome(num)) {
System.out.println(num);
break
;
}
else
if
(num > 4294967295l) {
System.out.println(
"No palindrome exist"
);
}
}
}
public
static
void
main(String[] args)
{
ReverseAdd ob =
new
ReverseAdd();
ob.ReverseandAdd(195l);
ob.ReverseandAdd(265l);
}
}
Python3
def
reverse Digits(num):
rev_num
=
0
while
(num >
0
):
rev_num
=
rev_num
*
10
+
num
%
10
num
=
num
/
/
10
return
rev_num
def
isPalindrome(num):
return
(reverse Digits(num)
=
=
num)
def
ReverseandAdd(num):
rev_num
=
0
while
(num <
=
4294967295
):
rev_num
=
reverse Digits(num)
num
=
num
+
rev_num
if
(isPalindrome(num)):
print
(num)
break
else
:
if
(num >
4294967295
):
print
(
"No palindrome exist"
)
ReverseandAdd(
195
)
ReverseandAdd(
265
)
C#
using
System;
class
GFG {
static
long
reverse Digits(
long
num)
{
long
rev_num = 0;
while
(num > 0) {
rev_num = rev_num * 10 + num % 10;
num = num / 10;
}
return
rev_num;
}
static
bool
isPalindrome(
long
num)
{
return
(reverse Digits(num) == num);
}
static
void
ReverseandAdd(
long
num)
{
long
rev_num = 0;
while
(num <= 4294967295) {
rev_num = reverse Digits(num);
num = num + rev_num;
if
(isPalindrome(num)) {
Console.WriteLine(num);
break
;
}
else
if
(num > 4294967295) {
Console.WriteLine(
"No palindrome exist"
);
}
}
}
public
static
void
Main()
{
ReverseandAdd(195);
ReverseandAdd(265);
}
}
PHP
<?php
function
reverse Digits(
$num
)
{
$rev_num
= 0;
while
(
$num
> 0)
{
$rev_num
=
$rev_num
* 10 +
$num
% 10;
$num
= (int)(
$num
/ 10);
}
return
$rev_num
;
}
function
isPalindrome(
$num
)
{
return
(reverse Digits(
$num
) ==
$num
);
}
function
ReverseandAdd(
$num
)
{
$rev_num
= 0;
while
(
$num
<= 4294967295)
{
$rev_num
= reverse Digits(
$num
);
$num
=
$num
+
$rev_num
;
if
(isPalindrome(
$num
))
{
print
(
$num
.
"\n"
);
break
;
}
else
if
(
$num
> 4294967295)
{
print
(
"No palindrome exist"
);
}
}
}
ReverseandAdd(195);
ReverseandAdd(265);
?>
Javascript
<script>
function
reverse Digits(num)
{
let rev_num = 0;
while
(num > 0)
{
rev_num = rev_num * 10 + num % 10;
num = parseInt(num / 10, 10);
}
return
rev_num;
}
function
isPalindrome(num)
{
return
(reverse Digits(num) == num);
}
function
ReverseandAdd(num)
{
let rev_num = 0;
while
(num <= 4294967295)
{
rev_num = reverse Digits(num);
num = num + rev_num;
if
(isPalindrome(num))
{
document.write(num +
"</br>"
);
break
;
}
else
if
(num > 4294967295)
{
document.write(
"No palindrome exist"
+
"</br>"
);
}
}
}
ReverseandAdd(195);
ReverseandAdd(265);
</script>
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.
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . 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.