Javatpoint Logo
Javatpoint Logo

Adam Number in C Program

In C programming, an Adam number is a number for which the square of its reverse is equal to the reverse of the square of the number. More formally, an Adam number is a positive integer "n" such that:

where "^" represents the exponentiation operator and "reverse(n)" represents the number obtained by reversing the digits of "n". For example, 12 is an Adam number because:

Therefore, 12 is an Adam number. To check if a given number is an Adam number, we need to perform the following steps:

  1. Compute the square of the number n.
  2. Reverse the digits of n and compute its square.
  3. Reverse the digits of the square obtained in step 2.
  4. Compare the result obtained in step 1 with the result obtained in step 3. If they are equal, then n is an Adam number; otherwise, it is not.

In C programming, we can implement this algorithm using basic arithmetic operations and a loop to reverse the digits of the number. Here is an example code:

C Code

Output

This code prompts the user to input an integer number and then checks whether it is an Adam number using the "isAdam" function defined in the code.

If the entered number is an Adam number, the program will output:

Enter a number: [number entered by user]
[number entered by user] is an Adam number.

If the entered number is not an Adam number, the program will output:

Enter a number: [number entered by user]
[number entered by user] is not an Adam number.

For example, if the user enters 12, which is an Adam number, the program will output:

Enter a number: [number entered by user]
[number entered by user] is not an Adam number.

If the user enters 5, which is not an Adam number, the program will output:

Enter a number: 12
12 is an Adam number.

Explanation:

In the first case, the user enters 12. The program checks if 12 is an Adam number by calling the "isAdam" function. The function first calculates the square of 12, which is 144. It then calculates the reverse of 12, which is 21, and the reverse of 144, which is 441. The function then calculates the square of 441, which is 194481. Finally, it compares the values of 144 and 194481. Since they are equal, the function returns true, indicating that 12 is an Adam number. The program then outputs the message "12 is an Adam number."

In the second case, the user enters 5. The program checks if 5 is an Adam number by calling the "isAdam" function. The function first calculates the square of 5, which is 25. It then calculates the reverse of 5, which is 5, and the reverse of 25, which is 52. The function then calculates the square of 52, which is 2704. Finally, it compares the values of 25 and 2704. Since they are not equal, the function returns false, indicating that 5 is not an Adam number. The program then outputs the message "5 is not an Adam number."

In both cases, the program uses the same approach to determine if the entered number is an Adam number. It calculates the square of the number, the reverse of the number, the reverse of the square of the number, and the square of the reverse of the square of the number. It then compares the square of the number with the square of the reverse of the square of the number. If they are equal, the number is an Adam number; otherwise, it is not.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA