C程序打印从1到n的所有强数字

时间:2017-01-23 08:35:34

标签: c

我写了这段代码来打印从1到n的所有强数字,但根本没有得到输出。终端在运行程序后卡住了,我不知道哪里出错了。请纠正我。

145是一个很强的数字,因为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);
    }
}

5 个答案:

答案 0 :(得分: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();
}

使用增量运算符,即i++,并将总和与原始数字进行比较。

答案 1 :(得分:0)

j=j/10; 

正在影响j。然后,将此修改后的j与

中的和进行比较
if(j==sum)
  printf("\n%d\n",j);

答案 2 :(得分:0)

由于您在另一个循环中使用了循环变量,您的代码会陷入无限循环。 for循环的变量j在每次迭代中递增1。但同样在同一次迭代中,你将j除以10,直到它为0.这就是为什么j在你的for循环的每次迭代开始时始终为1。

解决方案是简单地为while循环使用额外变量,并将其初始化为该迭代的j值。请参阅下面的代码解决此问题。

ImageView

答案 3 :(得分:0)

您的程序失败,因为您修改循环内的循环计数器j以枚举其数字。您也忘记在循环内重新初始化i

这是一个更简单,更快速的版本:

#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;
}

答案 4 :(得分:0)

代码在c中找到高达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;
}