closed 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 by KAUSTAV (720 points)
closed Jun 30, 2018 by KAUSTAV
#include <stdio.h>

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

1 Answer

0 votes
answered Jun 14, 2018 by Roger Schrader (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 );

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.