0

I wrote this code to print all the strong numbers from 1 to n, but am not getting the output at all. The terminal is getting stuck after running the program, I have no idea where am wrong. Please correct me.

145 is a strong number, since 1! + 4! + 5! == 145

#include <stdio.h>

void main() {
    int i = 1, fact, sum, n, a;
    long int number;

    printf("\n Find Strong numbers between 1 to \n");
    scanf("\n%ld", &number);

    printf("\n All Strong numbers between 1 to %ld are:\n", number);

    for (int j = 1; j <= number; j++) {   
        sum = 0;

        while (j != 0) {
            a = j % 10;
            j = j / 10;
            fact = 1; 

            while (i <= a) {   
                fact = fact * a;
                a--;
            }
            sum = sum + fact;
        }
        if (j == sum)
            printf("\n%d\n", j);
    }
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
ChDlGy
  • 67
  • 2
  • 8
  • Where you are fundamentally going wrong is that the `for` loop iterates by increasing `j`, and the first inner `while` loop reduces `j` to zero. In combination, that makes for an infinite loop. It also means that `j == sum` will never be true, so no output is produced. In combination, that will make it appear the code is "stuck" - the program continually looping and producing no output. Lastly, `main()` returns `int`, not `void`. – Peter Jan 23 '17 at 09:36
  • I'm not convinced in using scanf with `\n`. Please see here, how to use scanf with long integers. [See here](http://stackoverflow.com/questions/2852390/using-scanf-in-c-c) – allevo Jan 23 '17 at 09:53

5 Answers5

1
#include <stdio.h>
#include <conio.h>

void main() {
    char next_time;
    int num, fact, n, sum = 0, i, value;
    next_time = 'y';
    while (next_time == 'y' || next_time == 'Y') {
        printf("Enter a number to check whether a number is strong or not:\t");
        scanf("%d", &num);
        value = num;

        while (num != 0) {
            fact = 1;
            n = num % 10;
            num = num / 10;
            for (i = 1; i <= n; i++) {
                fact *= i;
            }
            sum += fact;
        }
        if (sum == value)
            printf("%d is strong number", value);
        else
            printf("%d is not strong number", value);

        printf("\n\n******************************");
        printf("\n\nDo you want to start  again?");
        printf("\n\nEnter Y or y to to continue and any other key to exit:\t");
        scanf(" %c", &next_time);
        printf("\n*********************************************\n\n");
    }
    getch();
}

Use increment operator i.e. i++ and compare the sum with original number.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • Your program does not answer the question: *C program to print all the strong numbers from 1 to n* – chqrlie Jan 23 '17 at 09:45
0
j=j/10; 

is affecting j. You then compare this modified j with sum in

if(j==sum)
  printf("\n%d\n",j);
jimifiki
  • 5,377
  • 2
  • 34
  • 60
0

Your code is stuck in an infinite loop due to your usage of your loop variable inside another loop. The variable j of your for loop is incremented by 1 in each iteration. But also in that same iteration you divide j by 10 until it is 0. That's why j is always 1 at the start of each iteration of your for loop.

The solution is simply using an extra variable for your while loop and initializing it to the value of j for that iteration. See below for the code solving this.

void main()
{
    int i=1,fact,sum, n, a, tmp;
    long int number;

    printf("\n Find Strong numbers between 1 to \n");
    scanf("\n%ld",&number);

    printf("\n All Strong numbers between 1 to %ld are:\n",number);

    for(int j=1;j<=number;j++)
    {
        tmp = j;
        sum=0; 

        while(tmp!=0)
        {
            a=j%10;
            tmp=tmp/10;
            fact=1; 

            while(i<=a)
            {   
                fact=fact*a;
                a--;
            }
            sum=sum+fact;
        }
        if(j==sum)
            printf("\n%d\n",j);

    }
}
ilim
  • 4,477
  • 7
  • 27
  • 46
0

Your program fails because you modify the loop counter j inside the loop to enumerate its digits. You also forget to reinitialize i inside the loop.

Here is a simpler and faster version:

#include <stdio.h>

int main(void) {
    unsigned long int factorials[10];
    unsigned long int number;

    factorials[0] = 1;
    for (int i = 1; i < 10; i++) {
        factorials[i] = factorials[i - 1] * i;
    }

    printf("Find Strong numbers from 1 to ");
    scanf("%lu", &number);

    printf("\nAll Strong numbers between 1 to %ld are:\n", number);

    for (unsigned long int j = 1; j <= number; j++) {
        long int n, sum = 0;
        for (n = j; n > 9; n /= 10) {
            sum += factorials[n % 10];
        }
        sum += factorials[n];
        if (j == sum) {
            printf("%ld\n", j);
        }
    }
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
0

code in c to find strong numbers upto 100000 OUTPUT

#include<stdio.h>
int main() {
    int ino = 0;
    int newno = 0;
    int digit = 1;
    int fact = 1;
    int i;
    for(i = 0; i <= 100000; i++)
    {
        ino = i;
        newno = 0;
        while(ino != 0)
        {
            fact = 1;
            digit = ino % 10;
            while(digit > 1)
            {
                fact *= digit--;            
            }
            newno += fact; 
            ino /= 10;
        }

        if(i == newno)
        printf("%d, ",i);
    }
    printf("\b\b ");
return 0;
}