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

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

Problem Description

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.

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.

  1. #include<iostream>
  2. #include<math.h>
  3. using namespace std;
  4. int main ()
  5. {
  6.     int num, temp, r1, r2, sq, rev1 = 0, rev2 = 0;
  7.     cout << "Enter a number : ";
  8.     cin >> num;
  9.     temp = num * num;
  10.     while (temp != 0)
  11.     {
  12.         r1 = temp % 10;
  13.         rev1 = rev1 * 10 + r1;
  14.         temp = temp / 10;
  15.     }
  16.     sq = sqrt(rev1);
  17.     while (sq != 0)
  18.     {
  19.         r2 = sq % 10;
  20.         rev2 = rev2 * 10 + r2;
  21.         sq = sq / 10;
  22.     }
  23.     if (rev2 == num)
  24.         cout << "\n" << num << " is an Adam number.";
  25.     else
  26.         cout << "\n" << num << " is not an Adam number.";
  27.     return 0;
  28. }
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
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.

Sanfoundry Global Education & Learning Series – C++ Programs.

To practice all C++ programs, here is complete set of 1000+ C++ Programming examples.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.