For starters you should declare variables in the minimal scope where they are used. Otherwise it is difficult to read code.
Within this statement
for(n=1;i<=limit;n++)
^^^^^^^^
there is a typo. The variable i
is not initialized. You have to write
for ( n=1; n <= limit; n++ )
However in the next loop the variable n
is being changed.
while(n>0)
{
//...
n=n/10;
}
You need to use another variable in this loop. For example before the loop you could write
int value = n;
while ( value > 0 )
{
//...
}
This loop
for(i=r;i>=1;i--)
can have one iteration less
for(i=r;i > 1;i--)
Also it is better to use the type unsigned int
instead of the type int
. Otherwise the user can enter a negative number.
Here is a demonstrative program.
#include <stdio.h>
int main(void)
{
printf( "Enter the limit to find strong numbers: " );
unsigned int n = 0;
scanf( "%u", &n );
for ( unsigned int i = 0; i < n; i++ )
{
unsigned int value = i + 1;
unsigned int sum = 0;
do
{
unsigned int f = 1;
for ( unsigned int digit = value % 10; digit > 1; digit-- )
{
f *= digit;
}
sum += f;
} while ( value /= 10 );
if ( i + 1 == sum )
{
printf( "%u is a strong number.\n", i + 1 );
}
}
return 0;
}
Its output might look like
Enter the limit to find strong numbers: 145
1 is a strong number.
2 is a strong number.
145 is a strong number.