Cover Image for Adam Number in C Program
237 views

Adam Number in C Program

The Adam number, also known as a reversible digit in a square number, is a number for which the square of its reverse is the reverse of the square of the number itself. In other words, if you reverse the digits of an Adam number and square both the original number and its reverse, the resulting numbers should be reversals of each other.

Here’s a C program to check if a given number is an Adam number:

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

// Function to reverse the digits of a number
int reverse(int num) {
    int rev = 0;
    while (num > 0) {
        rev = rev * 10 + num % 10;
        num /= 10;
    }
    return rev;
}

// Function to check if a number is an Adam number
int isAdamNumber(int num) {
    int numSquared = num * num;
    int revNum = reverse(num);
    int revNumSquared = revNum * revNum;
    int revRevNumSquared = reverse(revNumSquared);

    return numSquared == revRevNumSquared;
}

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

This program defines two functions, reverse and isAdamNumber. The reverse function reverses the digits of a number, and the isAdamNumber function checks if a given number is an Adam number by comparing the squares and reversals of the squares. The main function reads a number from the user, calls isAdamNumber, and prints the result.

Compile and run this program, and it will tell you whether the input number is an Adam number or not.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS