C++ Program to Check if a Number is an Adam Number

The program takes a number and checks if it is an Adam number. A number when squared, reversed, its root is found and is reversed again is equal to the original number, then it is called an Adam number.

Problem Solution

1. The number to be checked is entered.
2. It is squared and reversed.
3. Square root of the reversed number is found.
4. It is reversed again and compared with the original number.
5. If they are same, the number entered is an Adam number, else not.
6. Then result is printed.
7. Exit.

advertisement

C++ Program/Source code

Here is the source code of C++ Program to Check if a Number is an Adam Number. The program output is shown below.

#include<iostream>

#include<math.h>

usingnamespace std;

int main ()

{

int num, temp, r1, r2, sq, rev1 =0, rev2 =0;

cout<<"Enter a number : ";

cin>> num;

temp = num * num;

while(temp !=0)

{

r1 = temp %10;

rev1 = rev1 *10+ r1;

temp = temp /10;

}

sq =sqrt(rev1);

while(sq !=0)

{

r2 = sq %10;

rev2 = rev2 *10+ r2;

sq = sq /10;

}

if(rev2 == num)

cout<<"\n"<< num <<" is an Adam number.";

else

cout<<"\n"<< num <<" is not an Adam number.";

return0;

}

Program Explanation

1. The user is asked to enter the number to be checked and stored in the variable ‘num’.
2. The variables ‘rev1’ and ‘rev2’ are initialized as 0.
3. The square of the original number is stored in the variable ‘temp’.
4. The reverse of temp is calculated and stored in rev1.
5. The square root of rev1 is calculated using the function ‘sqrt’ under math.h library, and its reverse is stored in rev2.
6. If rev2 is equal to the original number entered i.e. num, then it is an Adam number, else not.
7. The result is then printed.

advertisement

Runtime Test Cases

Case 1 :
Enter a number : 12
12 is an Adam number.
Case 2 :
Enter a number : 200
200 is not an Adam number.
Case 3 :
Enter a number : 0
0 is an Adam number.