## Introduction

This blog will discuss how to write a program to calculate the magic number in C.

Also see, __Binary to Hex Converter____,____C Static Function__

### Problem Statement

We are given a number, and we need to check whether the given number is a magic number or not. A number is said to be a magic number if the product of sum of its digits and the reverse of the sum is equal to the given number.

### Example

```
1729 is a magic number.
Since, 1+7+2+9=19 and 19*91=1729.
183 is not a magic number.
Since, 1+8+3=12 and 12*21 != 183
```

## Approach

The** approach** to write a program to check whether the number is magic number or not. First we need to find the sum of the digits. Then we need to reverse the obtained sum. If the product of the sum and the reverse of the sum is equal to the given number, then the number is said to be a magic number.

Time Complexity for writing a program to check magic number = O(n), n is the number of digits in the given number.

Till now, I assume you must have got the basic idea of what has been asked in the problem statement. So, I strongly recommend you first give it a try.

Must Read __ C Program to Reverse a Number__

**PseudoCode**

```
___________________________________________________________________
procedure magicnumber():
___________________________________________________________________
1. Declare n, temp, rev, digit, sum
2. Take a number as user input
3. temp=n
4. While temp>0:
sum = sum + temp % 10;
temp = temp / 10;
5. temp = sum
6. Print digitsum
7. while ( temp > 0):
rev = rev * 10 + temp % 10;
temp = temp / 10;
8. Print reverse sum
9. Find product of sum and reverse of sum
10. If rev * sum == n:
Print magic number
Else
Print not a magic number.
end procedure
___________________________________________________________________
```

### Implementation in C

```
#include <stdio.h>
#include <conio.h>
int main ()
{
int n, temp, rev = 0, digit, sum = 0;
printf (" Enter a Number: \n");
scanf (" %d", &n);
// assigning the number to temporary variable
temp = n;
// calculating the sum of digits
while ( temp > 0)
{
// extracting digits and calculating the sum
sum = sum + temp % 10;
temp = temp / 10;
}
//storing the sum
temp = sum;
printf (" \n digit sum = %d", temp);
// reverse sum of given digits
while ( temp > 0)
{
rev = rev * 10 + temp % 10;
temp = temp / 10;
}
printf (" \n The reverse of sum = %d", rev);
printf (" \n The product of %d * %d = %d", sum, rev, rev * sum);
// checking for magic number
if ( rev * sum == n)
{
printf (" \n %d is a Magic Number. ", n);
}
else
{
printf (" \n %d is not a Magic Number. ", n);
}
return 0;
}
```

**Output:**

```
Sample Input:
Enter a Number:
1729
Sample Output:
digit sum = 19
The reverse of sum = 91
The product of 19 * 91 = 1729
1729 is a Magic Number.
```

You can try code by yourself on online C editor.

### Complexity Analysis

**Time Complexity:** **O(n).**

**Analysing Time Complexity:**

N here refers to the number of digits in a number.

**Space complexity**: O(1), no extra variable used.

You can also read about __dynamic array in c____, __and __Tribonacci Series____ __**You can also read about - **Strong number in c

Must Read __what is storage class in c__ and** **__Decision Making in C__