# the following code was supposed to display all strong numbers in a given limit but it is not giving any output, why?

–1 vote
asked Jun 7, 2018
closed Jun 30, 2018
#include <stdio.h>

int main()
{
int n,i,r,sum =0,fact,limit;         //  it doesn't display any results
printf("Enter number = ");
scanf("%d",&limit);
for(n=1;n<=limit;n++)
{
while(n>0)
{
r = n%10;
fact = 1;
for(i=r;i>=1;i--)
{
fact = fact*i;
}
sum = sum + fact;
n= n/10;
}
if(n==sum)
printf("%d strong number",n);
}
}
closed with the note: the answer by the below user is sufficient

answered Jun 14, 2018 by (140 points)
First, you have an infinite loop in your program.

trace the value of n,  first it is 1, then the while loop sets it to zero again and then adds 1 to in the for loop

Second, you never reset your accumulator to zero.

Third, your check is comparing n, which is zero, to your sum, which can never be zero because 0! = 1.

try this:

for( num = 1; num <= limit; num++)

{

n = num;

sum = 0;

// your while loop

if( num == sum )

printf( "%d is a strong number\n", num );

}