Adam Number


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.

C/C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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");    
    }
  
}

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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
?>

chevron_right



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 contribute.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.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Improved By : vt_m, jit_t

Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.