Problem: Write a C program to check whether a number is Magic Number or not.
A magic number is that number whose sum of the digits is when multiplied by the reverse of the same sum results back the original number.
Example: 1729

Steps to Check Magic Number in C
- Input a number.
- Calculate the sum of its digits.
- Multiply the computed sum with its reverse.
- check if the product is equal to the input number.
- If Yes, then the given number is Magic number else not.
Here is the implementation of same in C.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | #include <stdio.h> #include <stdlib.h> int main() { int num, temp, rev=0, digit, sumOfDigits=0; printf("Enter a Number \n"); scanf("%d",&num); temp = num; //Calculating Sum of digits while(temp > 0){ //Extract digit and add them sumOfDigits += temp % 10; temp = temp / 10; } temp = sumOfDigits; //Compute reverse of Sum of Digits while( temp > 0){ rev = rev*10 + temp % 10; temp = temp / 10; } if(rev*sumOfDigits == num) printf("Magic Number \n"); else printf("Not a Magic Number \n"); return 0; } |
Output

In this tutorial, we learned to check magic number in C.