Adam Number Or Not In C Program

Example

int reverse(int num)
{
    int rev = 0;
    while(num > 0)
    {
        rev = rev * 10 + num % 10;
        num = num / 10;
    }
    return rev;
}

Next, our task involves creating a function that verifies whether a specified number qualifies as an Adam number. This can be achieved by initially calculating the square of the number and its reverse by leveraging the pow function sourced from the math.h library and the reverse function that we recently established. Subsequently, we can juxtapose the outcomes and validate if they remain identical upon digit reversal. Below is the implementation for the Adam number validation function:

Example

int isAdamNumber(int num)
{
    int sq = num * num;
    int rev = reverse(num);
    int rev_sq = rev * rev;
    int rev_rev_sq = reverse(rev_sq);
    if(sq == rev_rev_sq)
        return 1;
    else
        return 0;
}

Now, we have the ability to utilize the isAdamNumber function to verify whether a specified number is an Adam number or not. Below is the entire C program designed for this purpose:

Example

#include <stdio.h>
#include <math.h>

int reverse(int num)
{
    int rev = 0;
    while(num > 0)
    {
        rev = rev * 10 + num % 10;
        num = num / 10;
    }
    return rev;
}

int isAdamNumber(int num)
{
    int sq = num * num;
    int rev = reverse(num);
    int rev_sq = rev * rev;
    int rev_rev_sq = reverse(rev_sq);
    if(sq == rev_rev_sq)
        return 1;
    else
        return 0;
}

int main()
{
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    if(isAdamNumber(num))
        printf("%d is an Adam number\n", num);
    else
        printf("%d is not an Adam number\n", num);
    return 0;
}

In this software, the initial step involves obtaining a numerical input from the user. Subsequently, the isAdamNumber function is invoked to verify whether the input number qualifies as an Adam number. If the number meets the criteria of an Adam number, a corresponding notification is displayed. Conversely, if the number does not meet the criteria, an appropriate message indicating its non-Adam number status is presented.

In summary, an Adam number is a number where the square of the number is the reverse of the square of its reverse. We can verify whether a specific number is an Adam number by utilizing the C program outlined earlier.

Output

Output

12
Adam Number
5 
Not an Adam Number

Explanation:

The reverse function takes an integer as input and returns the reversed integer. It works by using a while loop to extract the last digit of the input integer using the modulus operator % and then remove the last digit using integer division by 10. The extracted digits are then concatenated in reverse order to form the reversed integer.

The isAdamNumber function accepts an integer parameter and outputs 1 if the number is classified as an Adam number; otherwise, it returns 0. Initially, the function computes the square of the given integer and the square of its reversed version using the pow function from the math.h library and a custom reverse function. Subsequently, it determines the reverse of the square of the reversed number by employing the reverse function once more. Ultimately, the function checks if the square of the original integer matches the reverse of the square of the reversed number, returning 1 if they are identical and 0 otherwise.

Input Required

This code uses input(). Please provide values below: