Print all Strong numbers between 1 to n with C.
/*
Strong number is a special number whose sum of factorial of digits
is equal to the original number.
\For example: 145 is strong number. Since, 1! + 4! + 5! = 145
*/
#include <stdio.h>
int main()
{
int i, j, cur, lastDigit, end;
long long fact, sum;
printf("Enter upper limit: ");
scanf("%d", &end);
printf("All Strong numbers between 1 to %d are:\n", end);
for (i = 1; i <= end; i++)
{
cur = i;
sum = 0;
while (cur > 0)
{
fact = 1;
lastDigit = cur % 10;
/* Find factorial of last digit of current num. */
for (j = 1; j <= lastDigit; j++)
{
fact = fact * j;
}
sum += fact;
cur /= 10;
}
if (sum == i)
{
printf("%d, ", i);
}
}
return 0;
}
Output
Enter upper limit : 145
All Strong numbers between 1 to 145 are :
1, 2, 145,
0 Comments