Cover Image for Adam Number or not in C Program
76 views

Adam Number or not in C Program

The Adam number, also known as a Friedman number, is a number for which the square of its digits is the reverse of the square of the number itself. In other words, if “abcd” is the number, and “wxyz” is its square, then “wxyz” should be the reverse of “abcd.” For example, 12 is an Adam number because 12^2 = 144, and if you reverse 144, you get 441, which is also the square of 21.

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

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

// Function to reverse 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
bool isAdamNumber(int num) {
    int square = num * num;
    int reverseSquare = reverse(square);
    int reverseNum = reverse(num);
    return square == reverseSquare && num != 0 && reverseNum != 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;
}

This program defines a reverse function to reverse an integer and an isAdamNumber function to check if a number is an Adam number based on the given criteria. It takes a number as input, calculates its square, reverses both the square and the number itself, and compares them to determine if it’s an Adam number.

Compile and run the 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