Answer to Question #265265 in C for Rosie

Question #265265

Write a C program to reverse the digits of a given number and add it to the original, If the sum is not a palindrome repeat this procedure. 

1
Expert's answer
2021-11-13T23:51:36-0500
#include <stdio.h>

int Reversed(int n) {
  int reversed = 0;
  while (n) {
    reversed *= 10;
    reversed += n % 10;
    n /= 10;
  }
  return reversed;
}

int Magic(int n) {
  int sum = n + Reversed(n);
  if (Reversed(sum) != sum) {
    return Magic(sum);
  }
  return sum;
}

int main() {
  int n;
  scanf("%d", &n);
  int res = Magic(n);
  printf("final: %d", res);
  return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS