C++ Adam Number


Adam Number is a number whose square is reverse of the square of its reverse.

Concept Explained − For a number to be adam number, the square of number is reverse of the square of the reverse of the number. Let’s take an example,

12 is the number. Square of 12 is 144 and the reverse of 12 is 21. The square of reverse of 12 i.e. 21 is 441. 441 is the reverse of 144 which is the square of 12.

Algorithm to check if a number is adam number −

  • Given the number xy, find the square of the number (xy)2.
  • For xy reverse the digits of the number -> yx.
  • Now, for the number yx, find the square of the number (xy)2.
  • Reverse the digits of (xy)2 and evaluate with (yx)2.
  • If both are equal, then the number is adam number.

Example

 Live Demo

#include <iostream>
using namespace std;
int reverseDigits(int num) {
   int rev = 0;
   while (num > 0) {
      rev = rev * 10 + num % 10;
      num /= 10;
   }
   return rev;
}
int main() {
   int num = 31;
   cout<<num<<" is ";
   int rev = reverseDigits(num);
   if ( (num*num) == (reverseDigits(rev*rev)) )
      cout << "Adam Number";
   else
      cout << "not an Adam Number";
   return 0;
}

Output

31 is Adam Number

Updated on: 07-Oct-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements