Learn how to Find Magic Number in C Programming. A Magic Integer is also known as Ramanujan’s Number. Find more about magic integers on Wikipedia. You must know how to Reverse the Digits of a Number and how to find the Sum of Digits of a Number to understand this program better.
What is a Magic Number?
Also known as Hardy-Ramanujan Number or Taxicab Number, it is an integer in which, when its digits are added, it produces a sum which, when multiplied by the its reversed number, fetches the original integer.
Must Read: C Program To Find if a Number is a Strong Integer or Not
Example
81
8 + 1 = 9
9 × 9 = 81
1729
1 + 7 + 2 + 9 = 19
19 × 91 = 1729
How To Check if an Integer is a Magic Number or Not?
- Fetch a Number from the User.
- Find Sum of Digits and Multiply it with the its Reverse.
- If the Product is same as the original number, it’s a Magic Number.
Steps:
- Input a Number and store it in N.
- Find Sum of Digits of the Original Number and store it in A.
- Reverse the Digits of A and store it in B.
- Multiply A and B and store it in C.
- If C == N, then it is a Magic Integer.
Must Read: C Program To Find Sum of Two Complex Numbers
C Program To Check if a Number is a Magic Number or Not
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | #include <stdio.h> int sum_of_digits(int); int reverse_digits(int); int main() { int sum, num, reverse; printf("\nEnter a Number:\t"); scanf("%d", &num); sum = sum_of_digits(num); if(sum < 10) { if((sum * sum) == num) { printf("\n%d is a Magic Integer\n", num); } else { printf("\n%d is Not a Magic Integer\n", num); } return 0; } reverse = reverse_digits(sum); if((sum * reverse) == num) { printf("\n%d is a Magic Integer\n", num); } else { printf("\n%d is Not a Magic Integer\n", num); } return 0; } int sum_of_digits(int num) { int sum = 0; while(num > 0) { sum = sum + (num % 10); num = num / 10; } return sum; } int reverse_digits(int num) { int reverse = 0; while(num > 0) { reverse = (reverse * 10) + (num % 10); num = num / 10; } return reverse; } |
Must Read: C Program To Check if Number is Narcissistic Number or Not
Output

If you have any compilation errors or doubts in this code to find magic integers in c programming, let us know about it in the comment section below.
This is just awesome! I just saw the movie – The Man Who Knew Infinity. Once G H Hardy and S Ramanujan were travelling on a motor vehicle and this was inscribed on the Number plate of that vehicle – CE 1729. Just see the brilliant minds and their calculations! It is astonishing. Good work!
Excellent. Thanks for the magic integer c program!!