-1

Basci Program My logic is not working properly to "find the strong number" of given range. Nothing is printing even after giving range of number.

#include<stdio.h>
int main()
{
    int n,r,sum,fact,limit,i;
     printf("Enter the limit to find strong numbers");
    scanf("%d",&limit);
    for(n=1;i<=limit;n++)
    {
        sum=0;
        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 is a strong number\n", n);
    }
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Gokul UK
  • 49
  • 8

2 Answers2

0

There are a couple of issues:

  1. You are missing a return 0 statement at the end of the program for the main function (though this is not required by the C99 and above standards as said in the comments)
  2. You are missing the main function end brace
  3. As said in the comments, your for loop condition is wrong for(n=1;i<=limit;n++) it should compare n instead of i like such for(n=1;n<=limit;n++)
  4. You are overwriting the n number counter here n=n/10;. Instead you should you another variable to break down the number into digits like such before the while loop int num = n;
  5. Lastly, it would be useful to print if a number is not a strong number with an else clause in the last if statement like such else printf("%d is not a strong number", n);

Here is the correct program:

#include <stdio.h>                                         
int main()                                                 
{                                                          
    int n, r, sum, fact, limit, i;                         

    printf("Enter the limit to find strong numbers");      

    scanf("%d", &limit);                                   

    for (n = 1; n <= limit; n++) // Point 3
    {                                                      
        sum = 0;                                           

        int num = n; // Point 4

        while (num > 0)                                    
        {                                                  
            r = num % 10;                                  

            fact = 1;                                      

            for (i = r; i >= 1; i--)                       

            {                                              
                fact = fact * i;                           
            }                                              

            sum = sum + fact;                              

            num = num / 10;                                
        }                                                  

        if (n == sum) printf("%d is a strong number\n", n);
        else printf("%d is not a strong number\n", n); // Point 5
    }                                                      

    return 0; // Point 1
} // Point 2
John
  • 1,012
  • 14
  • 22
  • Regarding point 1, since the C99 standard if the `main` function misses an explicit `return` statement or `exit` call, the C compiler adds an implicit `return 0;` at the end. – Some programmer dude Oct 04 '19 at 14:28
  • @Someprogrammerdude Good point. I didn't know that. Could you append the section in the standard where is says that? – John Oct 04 '19 at 14:30
  • @Someprogrammerdude I think I found it https://stackoverflow.com/questions/4138649/why-is-return-0-optional – John Oct 04 '19 at 14:40
  • @john well corrected code. Its working now. I only need the list of strong number in the given range. Hence I removed the else statement // Point 5. – Gokul UK Oct 04 '19 at 14:51
0

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.
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335