Adam Number - GeeksforGeeks

27 Jan, 2022
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++
Java
Python3
C#
PHP
Javascript
// CPP program to check Adam Number#include <bits/stdc++.h>using namespace std; // To reverse Digits of numbersint reverseDigits(int num){    int rev = 0;    while (num > 0)    {        rev = rev * 10 + num % 10;        num /= 10;    }    return rev;} // To square numberint square(int num){    return (num * num);} // To check Adam Numberbool 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 functionsint main(){    int num = 12;         if (checkAdamNumber(num))    cout << "Adam Number";    else    cout << "Not a Adam Number";            return 0;}
// Java program to check Adam Numberpublic 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 program to check Adam Number# To reverse Digits of numbersdef reverseDigits(num) :    rev = 0    while (num > 0) :        rev = rev * 10 + num % 10        num //= 10             return rev # To square numberdef square(num) :    return (num * num)     # To check Adam Numberdef 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 functionsnum = 13if (checkAdamNumber(num)) :    print ("Adam Number")else :    print ("Not a Adam Number")  # This code is contributed by Nikita Tiwari
// C# Program to Check Adam Numberusing 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 program to check Adam Number // To reverse Digits of numbersfunction reverseDigits($num){    $rev = 0;    while ($num > 0)    {        $rev = $rev * 10 + $num % 10;        $num = (int) $num / 10;    }    return $rev;} // To square numberfunction square($num){    return ($num * $num);} // To check Adam Numberfunction 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";elseecho "Not a Adam Number"; // This code is contributed by aj_36?>
<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
 
YouTubeGeeksforGeeks501K subscribersAdam Number | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 4:12•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=7VyaFj-SYlE" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
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 review-team@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. 
vt_m
jit_t
divyeshrabadiya07
amartyaghoshgfg
Mathematical
School Programming
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Program to find sum of elements in a given array
Program for factorial of a number
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
Interfaces in Java