Table of contents
1.
Introduction
1.1.
Problem Statement
1.2.
Example
2.
Approach 
2.1.
PseudoCode
2.2.
Implementation in C
2.3.
Complexity Analysis
3.
Frequently Asked Question
3.1.
Is there any other method to check whether number is a magic number besides the one discussed in this blog?
3.2.
What is a modulus operator?
3.3.
What is time complexity? What is space complexity?
4.
Conclusion
Last Updated: Dec 11, 2024

Magic number in C

Author Urwashi Priya
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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.

try here

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;  
      
}  
You can also try this code with Online C Compiler
Run Code

 

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

Frequently Asked Question

Is there any other method to check whether number is a magic number besides the one discussed in this blog?

Yes, we can check if the number is a magic number using the recursive approach. A number is a magic number if on dividing the number by 9, the remainder is 1.

What is a modulus operator?

The modulus operator is used to calculate the remainder.
Example: 7%9=7, 10%3=1.

What is time complexity? What is space complexity?

Time complexity measures the time taken by a computer to run an algorithm. It is of three types: best time complexity, average time complexity and worst time complexity. Space complexity defines the amount of extra space used in an algorithm.

Conclusion

This article taught us how to write a program to calculate the magic number in C and discussed its implementation. We hope you could take away critical techniques like analysing problems by walking over the execution of the steps and finding out the pattern followed in most basic problems.

 

Live masterclass