Steps to find the Magic number in C
Begin by defining a variable of integer type and prompting the user to input a numerical value, which will then be stored in a variable named N.
Step 2: Calculate the total sum of all the digits in the initial number and save this value in a separate variable named A.
Reverse the value of variable A and store it in variable B.
Step 4: Next, perform a multiplication operation on the variables A and B, and then save the result in variable C.
Verify if the value of C is equal to N, indicating that the given number is classified as a Magic Number. Otherwise, the number does not meet the criteria to be considered a Magic Number.
Example 1: Code to verify if a given number is a Magic number in the C programming language
Let's develop a software application that prompts the user to input a number and then verifies if the provided number qualifies as a magic number in the C programming language.
#include <stdio.h>
#include <conio.h>
int main ()
{
// declare integer variables
int n, temp, rev = 0, digit, sum_of_digits = 0;
printf (" Enter a Number: \n");
scanf (" %d", &n); // get the number
temp = n; // assign the number to temp variable
// use while loop to calculate the sum of digits
while ( temp > 0)
{
// extract digit one by one and store into the sum_of_digits
sum_of_digits = sum_of_digits + temp % 10; /* use modulus symbol to get the remainder of each iteration by temp % 10 */
temp = temp / 10;
}
temp = sum_of_digits; // assign the sum_of_digits to temp variable
printf (" \n The sum of the digits = %d", temp);
// get the reverse sum of given digits
while ( temp > 0)
{
rev = rev * 10 + temp % 10;
temp = temp / 10;
}
printf (" \n The reverse of the digits = %d", rev);
printf (" \n The product of %d * %d = %d", sum_of_digits, rev, rev * sum_of_digits);
// use if else statement to check the magic number
if ( rev * sum_of_digits == n)
{
printf (" \n %d is a Magic Number. ", n);
}
else
{
printf (" \n %d is not a Magic Number. ", n);
}
return 0;
}
When running the aforementioned program in the C compiler, it generates the following result on the console display.
Enter a number
1729
The sum of the digits = 19
The reverse of the digits = 91
The product of 19 * 91 = 1729
1729 is a Magic Number.
In the given code snippet, we prompt the user to input the number 1729 and store it in a temporary variable. Subsequently, a while loop is employed to calculate the sum of the individual digits (n) of the number and store the total in the 'sumofdigits' variable before updating the temporary variable. Another iteration of the loop is used to reverse the sum of the digits and display the reversed number. Finally, a comparison is made between the original number (N) and the product of 'sumofdigits' and the reversed number ('rev') to demonstrate the concept of a magic number.
Example 2: Script to verify if a given number qualifies as a Magic number utilizing a custom function
Let's demonstrate a sample code to prompt the user for a number input and verify if the provided number qualifies as a magic number in the C programming language.
/* program to check the magic number using the user-defined function in c programming. */
#include <stdio.h>
#include <conio.h>
// create getReverse () function to get the reverse of the given number
int getReverse (int num)
{
int revNum = 0;
//use while loop to get the reverse of original nummber (n)
while ( num > 0)
{
revNum = (revNum * 10) + (num % 10);
num = num / 10;
}
printf (" \n The reverse of the number = %d", revNum);
return revNum;
}
// create getSum() function to get the sum of each digits of the given number
int getSum ( int num)
{
int sum = 0; // initialize the sum variable with 0
while ( num > 0)
{
sum = sum + ( num % 10);
num = num / 10;
}
printf ( " \n The sum of the given number = %d", sum);
return sum;
}
int main ()
{
// declare variable
int num, sum, revNum;
printf (" Enter the number: ");
scanf (" %d", &num);
sum = getSum (num); // call getSum () function
revNum = getReverse (sum); // callgetReverse () function and argument sum
int result = sum * revNum;
printf (" \n The product of %d and %d = %d ", sum, revNum, result);
// use if else to check condition
if (sum * revNum == num)
{
printf (" \n %d is a magic number. ", num);
}
else
{
printf ( " \n %d is not a magic number. ", num);
}
}
When running the aforementioned program in the C compiler, it generates the following result on your monitor.
Enter the number: 1729
The sum of the given number = 19
The reverse of the number = 91
The product of 19 and 91 = 1729
1729 is a Magic Number.
2 nd execution:
Enter the number: 5189
The sum of the given number = 23
The reverse of the number = 32
The product of 23 and 32 = 736
5189 is not a magic number.
In the preceding code snippet, we prompt the user to input the number 5189 and define two custom functions: getSum and getReverse. The getSum function is implemented to determine the total of the specified number, whereas the getReverse function is responsible for reversing the sum obtained from getSum. When the product of the getSum and getReverse functions equals the original number, it signifies that the number is classified as a magic number.
Method 2: Recursive Approach for Verifying the Magic Number in C
A numeric value is considered a magic number if the sum of its individual digits is repeatedly computed until a single digit, ultimately equaling 1. If this condition is not met, the number does not qualify as a magic number.
Example 1: Code snippet to determine the Magic number utilizing a recursive approach
Let's develop a C program that prompts the user to enter an integer, then checks if the input number qualifies as a magic number.
/* create a program to get the magic number by dividing the given number by 9. */
#include <stdio.h>
#include <conio.h>
int main ()
{
// declare integer variable
int num;
printf ( " Enter the number: ");
scanf (" %d", &num);
// get the modulus of the input number by 9 to check magic number
if ( num % 9 == 1)
{
printf ( " It is a Magic Number. ");
}
else
{
printf ( " it is not a Magic Number. ");
}
return 0;
}
Upon running the aforementioned program through the C compiler, it generates the following result on the console display.
Enter the number: 1789
It is a Magic Number.
2 nd execution:
Enter the number: 55057
It is not a Magic Number.