Skip to content
Related Articles

Related Articles

Improve Article

Adam Number

  • Difficulty Level : Basic
  • Last Updated : 04 May, 2021

Given a number n, write a program to check whether given number is Adam Number or not. 
Adam number is a number when reversed, the square of the number and the square of the reversed number should be numbers which are reverse of each other. Adam numbers upto 1000 are: 0, 1, 2, 3, 11, 12, 13, 21, 22, 31, 101, 102, 103, 111, 112 , 113, 121, 122, 201, 202, 211, 212, 221, 301, 311. 

Examples : 
 

Input : 12
Output : Adam Number
Explanation: 
Square of 12 = 144
Reverse of 12 = 21
Square of 21 = 441
Now Square of 12 and square of reverse of 12 are 
reverse of each other. Therefore 12 is Adam number.

Input : 14
Output : Not a Adam Number

Steps to find Adam number : 
 

Step 1:  Find square of number. 
Step 2:  Find reverse of number.
Step 3:  Find square of number in Step 2.
Step 4:  Find reverse of number in Step 3.
Step 5:  Find whether number obtained in Step 4 and Step 1 are equal or not.

 

Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution. 



 

 

C++




// CPP program to check Adam Number
#include <bits/stdc++.h>
using namespace std;
 
// To reverse Digits of numbers
int reverseDigits(int num)
{
    int rev = 0;
    while (num > 0)
    {
        rev = rev * 10 + num % 10;
        num /= 10;
    }
    return rev;
}
 
// To square number
int square(int num)
{
    return (num * num);
}
 
// To check Adam Number
bool checkAdamNumber(int num)
{
    // Square first number and square
    // reverse digits of second number
    int a = square(num);
    int b = square(reverseDigits(num));
     
    // If reverse of b equals a then given
    // number is Adam number
    if (a == reverseDigits(b))
    return true;
    return false;       
}
 
// Driver program to test above functions
int main()
{
    int num = 12;
     
    if (checkAdamNumber(num))
    cout << "Adam Number";
    else
    cout << "Not a Adam Number";   
     
    return 0;
}

Java




// Java program to check Adam Number
public class Main {
    // To reverse Digits of numbers
    static int reverseDigits(int num)
    {
        int rev = 0;
        while (num > 0)
        {
            rev = rev * 10 + num % 10;
            num /= 10;
        }
        return rev;
    }
 
    // To square number
    static int square(int num)
    {
        return (num * num);
    }
 
    // To check Adam Number
    static boolean checkAdamNumber(int num)
    {
        // Square first number and square
        // reverse digits of second number
        int a = square(num);
        int b = square(reverseDigits(num));
         
        // If reverse of b equals a then given
        // number is Adam number
        if (a == reverseDigits(b))
        return true;
        return false;       
    }
 
    // Driver program to test above functions
    public static void main(String[] args)
    {
        int num = 12;
         
        if (checkAdamNumber(num))
        System.out.println("Adam Number");
        else
        System.out.println("Not a Adam Number");   
    }
 
}

Python




# Python program to check Adam Number
# To reverse Digits of numbers
def reverseDigits(num) :
    rev = 0
    while (num > 0) :
        rev = rev * 10 + num % 10
        num /= 10
         
    return rev
 
# To square number
def square(num) :
    return (num * num)
     
# To check Adam Number
def checkAdamNumber(num) :
     
    # Square first number and square
    # reverse digits of second number
    a = square(num)
    b = square(reverseDigits(num))
         
    # If reverse of b equals a then given
    # number is Adam number
    if (a == reverseDigits(b)) :
        return True
    else :
        return False
 
# Driver program to test above functions
num = 13
if (checkAdamNumber(num)) :
    print "Adam Number"
else :
    print "Not a Adam Number"
 
 
# This code is contributed by Nikita Tiwari

C#




// C# Program to Check Adam Number
using System;
 
public class main {
 
    // To reverse Digits of numbers
    static int reverseDigits(int num)
    {
        int rev = 0;
        while (num > 0)
        {
            rev = rev * 10 + num % 10;
            num /= 10;
        }
        return rev;
    }
 
    // To square number
    static int square(int num)
    {
        return (num * num);
    }
 
    // To check Adam Number
    static bool checkAdamNumber(int num)
    {
        // Square first number and square
        // reverse digits of second number
        int a = square(num);
        int b = square(reverseDigits(num));
         
        // If reverse of b equals a then
        // given number is Adam number
        if (a == reverseDigits(b))
        return true;
        return false;
    }
 
    // Driver Code
    public static void Main(String []args)
    {
        int num = 12;
         
        if (checkAdamNumber(num))
        Console.WriteLine("Adam Number");
        else
        Console.WriteLine("Not a Adam Number");
    }
 
}
 
// This code is contributed by vt_m.

PHP




<?php
// PHP program to check Adam Number
 
// To reverse Digits of numbers
function reverseDigits($num)
{
    $rev = 0;
    while ($num > 0)
    {
        $rev = $rev * 10 + $num % 10;
        $num = (int) $num / 10;
    }
    return $rev;
}
 
// To square number
function square($num)
{
    return ($num * $num);
}
 
// To check Adam Number
function checkAdamNumber($num)
{
    // Square first number and square
    // reverse digits of second number
    $a = square($num);
    $b = square(reverseDigits($num));
     
    // If reverse of b equals a then
    // given number is Adam number
    if ($a == reverseDigits($b))
    return 0;
    return -1;
}
 
// Driver Code
$num = 12;
 
if (checkAdamNumber($num))
echo "Adam Number";
else
echo "Not a Adam Number";
 
// This code is contributed by aj_36
?>

Javascript




<script>
    // Javascript Program to Check Adam Number
     
    // To reverse Digits of numbers
    function reverseDigits(num)
    {
        let rev = 0;
        while (num > 0)
        {
            rev = rev * 10 + num % 10;
            num = parseInt(num / 10, 10);
        }
        return rev;
    }
   
    // To square number
    function square(num)
    {
        return (num * num);
    }
   
    // To check Adam Number
    function checkAdamNumber(num)
    {
        // Square first number and square
        // reverse digits of second number
        let a = square(num);
        let b = square(reverseDigits(num));
           
        // If reverse of b equals a then
        // given number is Adam number
        if (a == reverseDigits(b))
        return true;
        return false;
    }
     
    let num = 12;
           
    if (checkAdamNumber(num))
      document.write("Adam Number");
    else
      document.write("Not a Adam Number");
     
</script>

Output : 
 

Adam Number

 

This article is contributed by nuclode. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :