Q.9 WAP in C/ C++/ java to check whether the number is a Special Number or not.
Definition: If the sum of the factorial of digits of a
number (N) is equal to the number itself, the number (N) is called a special number.
INPUT : 145 The digits of the number are: 1, 4, 5
Factorial of digits:
1! = 1
4! = 4*3*2*1 = 24
5! = 5*4*3*2*1 = 120
Sum of factorial of digits = 1 + 24 +
120 = 145
Solution :
#include <stdio.h>
int factorial(int x) // function to return factorial of the number
{
int f = 1;
for (int i = 1; i <= x; i++)
{
f = f * i;
}
return f;
}
void main()
{
int n,sum,x;
printf("\nEnter any positive number : ");
scanf("%d", &n);
if (n < 0)
{
printf("\nInvalid input! Enter the value again -->");
main(); // calls the main() function again
}
else
{
x=n;
sum = 0;
while (x > 0)
{
sum += factorial(x % 10);
x = x / 10;
}
if(sum == n)
printf("%d is a Special Number\n",n);
else
printf("%d Not a special number\n",n);
}
} // end of main()
Comments
Post a Comment