Updated March 24, 2023

## Definition

In C programming language, a “magic number” refers to a number that satisfies a specific property: when the sum of its digits and the sum of the digits of its reverse are multiplied together, the result is equal to the original number. This property has been studied by mathematicians and is sometimes referred to as the “taxicab number”.

### Magic Number in C in Simple Language

In number theory, a magic number is a positive integer in which the sum of its digits, when multiplied with the reversed sum, yields the original number itself.

For example, the number 1458 is a magic number because:

- 1+4+5+8 = 18
- Reversed the value of 18 = 81
- 18*18 = 1458

Therefore, 1458 is a magic number since the sum of its digits, and its reverse multiplied together equals the original number.

Magic numbers in number theory are also known as “Pluperfect Digital Invariants” or “PPDI’s”. They have fascinated mathematicians for centuries and have been studied extensively in recreational mathematics.

### How to Find Magic Number in C?

- Accept a number Y from the user and store it in a variable.
- Determine the sum of the digits of the number Y and store it in a variable M. To do this, we can repeatedly extract the last digit of Y using the modulo operator and add it to M. We then divide Y by 10 to remove the last digit and continue the loop until Y becomes zero.
- Reverse the value of M and store it in a variable N. To do this, we can repeatedly extract the last digit of M using the modulo operator and add it to N. We then multiply N by 10 and continue the loop until M becomes zero.
- Calculate the product of values M and N, and assign the result to a variable O.
- Verify whether the value of Y is equal to the variable O. If the two values are equal, then Y is considered a magic number; otherwise, it is not a magic number.
- If the number is a magic number, then print a message saying that a number is a magic number. Otherwise, print a message saying that the number is not a magic number.

### Examples to Check Magic Number in C

Here are some examples to help you check for magic numbers in C code:

#### Example #1

**Code:**

```
#include <stdio.h>
int main()
{
// declare integer variables
int num, tmp, reverse = 0, digit_of_num, digitSum = 0 ;
printf("Enter a Number: \n");
scanf("%d", &num); // get the number
tmp = num; // assign the number to temp variable
// use while loop to calculate the sum of digits
while (tmp > 0)
{
// extract digit one by one and store into the digitSum
digitSum = digitSum + tmp % 10; /* use modulus symbol to get the remainder of each iteration by tmp % 10 */
tmp = tmp / 10;
}
tmp = digitSum; // assign the digitSum to tmp variable
printf("\n The sum of the digits = %d", tmp);
// get the reverse sum of given digits
while (tmp > 0)
{
reverse = reverse * 10 + tmp % 10;
tmp = tmp / 10;
}
printf("\n The reverse of the digits = %d", reverse);
printf("\n The product of %d * %d = %d", digitSum, reverse, reverse * digitSum);
// use if else statement to check the magic number
if (reverse * digitSum == num)
{
printf("\n %d is a Magic Number. ", num);
}
else
{
printf("\n %d is not a Magic Number. ", num);
}
return 0;
}
```

**Output:**

#### Example #2

**Code:**

```
#include <stdio.h>
// create getReverseNumber() function to get the reverse of the given number
int getReverseNumber (int number) {
int revNumber = 0;
//use while loop to get the reverse of original nummber (number)
while ( number > 0) {
revNumber = (revNumber * 10) + (number % 10);
number = number / 10;
}
printf (" \n The reverse of the number = %d", revNumber);
return revNumber;
}
// create getDigitSum() function to get the sum of each digits of the given number
int getDigitSum ( int number) {
int addition = 0; // initialize the addition variable with 0
while ( number > 0) {
addition = addition + ( number % 10);
number = number / 10;
}
printf ( " \n The sum of the given number = %d", addition);
return addition;
}
int main () {
// declare variable
int number, addition, revNumber;
printf (" Enter the number: ");
scanf ("%d", &number);
addition = getDigitSum (number); // call getDigitSum () function
revNumber = getReverseNumber (addition); // callgetReverseNumber () function and argument sum
int ans = addition * revNumber;
printf (" \n The product of %d and %d = %d ", addition, revNumber, ans);
// use if else to check condition
if (addition * revNumber == number) {
printf (" \n %d is a magic number. ", number);
} else {
printf ( " \n %d is not a magic number. ", number);
}
return 0;
}
```

**Output:**

### Conclusion

In conclusion, magic numbers are numbers that have a special property where the product of the sum of their digits and the reverse of that sum is equal to the original number. They have been studied for their interesting properties and relationships with other mathematical concepts, such as palindromic numbers and perfect numbers. In programming, magic numbers can be checked using a variety of algorithms and functions, such as those demonstrated in the examples provided. They can be used in various applications, such as cryptography, game development, and data validation. Overall, magic numbers are a fascinating concept in mathematics and programming that continue to intrigue and inspire further exploration.

### Recommended Article

We hope that this EDUCBA information on “Magic Number in C” was beneficial to you. You can view EDUCBA’s recommended articles for more information.