Find Magic Number C Program

By | October 10, 2016

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?

  1. Fetch a Number from the User.
  2. Find Sum of Digits and Multiply it with the its Reverse.
  3. If the Product is same as the original number, it’s a Magic Number.

Steps:

  1. Input a Number and store it in N.
  2. Find Sum of Digits of the Original Number and store it in A.
  3. Reverse the Digits of A and store it in B.
  4. Multiply A and B and store it in C.
  5. 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

#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

C Program To Find Magic Number using Functions

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.

2 thoughts on “Find Magic Number C Program

  1. Prabhjot Singh

    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!

    Reply

Let's Discuss