## Magic Number in CIn this section, we will discuss magic numbers in C programming language along with their various examples. When the sum of all the given digits of a number and the reverse of that sum is multiplied, which is equal to the original number, and then the number is called a magic number. For example, suppose we have a 1729 number and we need to validate whether the number is a magic number or not. So, first we need to get the sum of all digits of the number which is 19 (1 + 7 + 2 + 9 = 19). Reverse the sum of the number is 91, and then get the product of the sum of original digits with the reverse of that sum as 19 * 19 = 1729. So, it is the magic number.
Step 1: Declare an integer type variable and input a number from the user to store it in N. Step 2: Get the sum of all digits of the original number and store it another variable A. Step 3: Reverse the number of variable A and store into B. Step 4: After that, multiply the values of variable A and B and store into C. Step 5: Check whether C == N, then the number is a Magic Number. Else, the Number is not a Magic number.
Let's create a program to take a number from the user and validate whether the entered number is a magic number or not in the C programming language.
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 above program, we take 1729 from the user and assign the number to the temp variable. After that, use a while loop to extract the sum of the original digits (n) into sum_of_digits one by one and assign the result to the temp. Use the loop again to reverse the sum of the digits and print the reverse number. And finally compare the original digit (N) with the multiplication of sum_of_digits and rev variable that return the same result to show a magic number.
Let's create an example to take a number from the user and validate whether the entered number is a magic number or not in the C programming language.
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.
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 above program, we take 5189 from the user and create two user defined function getSum() and getRevere(). Where the getSum() function is used to calculate the sum of the given number, and the getReverse() function to reverse sum of the getSum() function. If the multiplication of the getSum() and getReverse()function is equal to the num, the number is a magic number.
A number is said to be a magic number, if the sum of the digits is recursively calculated till a single digit which is equal to 1. Otherwise the number is not a magic number
Let's create a program to input the integer data from the user and validate whether the number is a magic number or not in the C programming language.
Enter the number: 1789 It is a Magic Number.
Enter the number: 55057 It is not a Magic Number. |

For Videos Join Our Youtube Channel: Join Now

- Send your Feedback to [email protected]