Adam Number in C++



The Adam number is a number such that the square of the given number 'n' is the reverse of the square of the reverse of that number 'n'. In this article, our task is to write a program that can check whether the given number is an Adam number or not.

Here is an example to check whether 12 is an Adam number or not:

Input: num = 12
Output: 12 is an Adam number

Explanation:
num = 12 , square of num = 144
reverse of 12 = 21 , square of 21 = 441
Here, 144 is reverse of 441
=> 12 is an Adam number

Steps to Check Adam Number

The steps to check if the given number is an Adam number or not is mentioned below:

  • Take a number num, calculate its square value, and store it in squareNum.
  • Now, reverse the number using the reverseNum() function, calculate its square, and store it in squareRevNum.
  • Now check if the reversed square value squareRevNum is the reverse of the square of the num, i.e., squareNum.
  • If squareRevNum and squareNum are reverse of each other then the given number is an Adam number.

C++ Program to Check Adam Number

Here is the C++ code implementation of the above steps for checking Adam number:

#include <iostream>
using namespace std;

//Function to reverse a number
int reverseNum(int num)
{
   int res = 0;
   while (num != 0)
   {
      res = res * 10 + num % 10;
      num /= 10;
   }
   return res;
}

// For checking Adam number
bool checkNum(int num)
{
   int revNum = reverseNum(num);

   int squareNum = num * num;
   int squareRevNum = revNum * revNum;

   if (squareNum == reverseNum(squareRevNum))
   {
      return true;
   }
   return false;
}


int main()
{
   int num = 12;
   if (checkNum(num))
   {
      cout << "The number " << num << " is an Adam number";
   }
   else
   {
      cout << "The number " << num << " is not an Adam number";
   }
}

The output of the above code is as follows:

The number 12 is an Adam number

Conclusion

In this article, we discussed about the Adam number and understood the steps to implement it with a code example.

Updated on: 2025-07-11T18:16:36+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements