ADVERTISEMENT
Inputted Number is Magic or Not
A Magic Number is a number in which the product of the sum of its digits and the reverse of the sum is equal to the original number.
#include<stdio.h>
int GetSumOfDigits(int num);
int GetReverseOfNumber(int sumOfDigits);
main()
{
int num,sumOfDigits,reverseOfNumber;
printf("Enter number to find out magic number or not\n");
scanf("%d",&num);
sumOfDigits=GetSumOfDigits(num);
reverseOfNumber=GetReverseOfNumber(sumOfDigits);
if(sumOfDigits*reverseOfNumber==num)
{
printf("%d is a Magic Number\n",num);
}
else
{
printf("%d is not a Magic Number\n",num);
}
}
int GetSumOfDigits(int n)
{
int sum=0,x;
while(n>0)
{
x=n%10;
sum=sum+x;
n=n/10;
}
return sum;
}
int GetReverseOfNumber(int n)
{
int rev=0,x;
while(n>0)
{
x=n%10;
rev=rev*10+x;
n=n/10;
}
return rev;
}C Programming ExamplesEnter number to find out magic number or not
1729
1729 is a Magic Number
C Programming ExamplesNow, let’s break down the code step by step:
- Include Header File:
#include<stdio.h>
This line includes the standard input-output library in the program, which is necessary for using functions like printf and scanf.
- Function Prototypes:
int GetSumOfDigits(int num);
int GetReverseOfNumber(int sumOfDigits);
These lines declare the prototypes of two functions: GetSumOfDigits and GetReverseOfNumber. These functions will be defined later in the code.
- main() Function:
main()
The main function is the starting point of the program.
- Variable Declaration:
int num, sumOfDigits, reverseOfNumber;
These variables are used to store the user input, the sum of digits, and the reverse of the sum.
- User Input:
printf("Enter number to find out magic number or not\n");
scanf("%d", &num);
The user is prompted to enter a number, and the input is stored in the variable num.
- Function Calls:
sumOfDigits = GetSumOfDigits(num);
reverseOfNumber = GetReverseOfNumber(sumOfDigits);
The GetSumOfDigits function is called with the input num, and its result is stored in sumOfDigits. Similarly, the GetReverseOfNumber function is called with the input sumOfDigits, and its result is stored in reverseOfNumber.
- Magic Number Check:
if(sumOfDigits * reverseOfNumber == num)
{
printf("%d is a Magic Number\n", num);
}
else
{
printf("%d is not a Magic Number\n", num);
}
The program checks whether the product of sumOfDigits and reverseOfNumber is equal to the original number (num). If true, it prints that the number is a Magic Number; otherwise, it prints that it is not.
- GetSumOfDigits Function:
int GetSumOfDigits(int n)
{
int sum = 0, x;
while(n > 0)
{
x = n % 10;
sum = sum + x;
n = n / 10;
}
return sum;
}
This function takes an integer n as input and calculates the sum of its digits using a while loop.
- GetReverseOfNumber Function:
int GetReverseOfNumber(int n)
{
int rev = 0, x;
while(n > 0)
{
x = n % 10;
rev = rev * 10 + x;
n = n / 10;
}
return rev;
}
This function takes an integer n as input and calculates the reverse of the number using a while loop.
This program checks if a given number is a Magic Number by calculating the sum of its digits and the reverse of that sum. If the product of these two values is equal to the original number, it is considered a Magic Number.