一个强数是一个数字,其中各位数字的阶乘之和等于该数字本身。
示例123!= 1!+2!+3! =1+2+6 =9
在这个例子中,123不是一个强数,因为各位数字的阶乘之和不等于该数字本身。
145!=1!+4!+5! =1+24+120
=145
在这个例子中,145是一个强数,因为各位数字的阶乘之和等于该数字本身。
我们用以下逻辑来判断给定的数字是否是强数:
while(n){ i = 1,fact = 1; rem = n % 10; while(i <= rem){ fact = fact * i; i++; } sum = sum + fact; n = n / 10;}if(sum == temp) printf("%d is a strong number
",temp);else printf("%d is not a strong number
",temp);
程序以下是用于判断给定数字是否为强数的c程序:
在线演示
#include<stdio.h>int main(){ int n,i; int fact,rem; printf("
enter a number : "); scanf("%d",&n); printf("
"); int sum = 0; int temp = n; while(n){ i = 1,fact = 1; rem = n % 10; while(i <= rem){ fact = fact * i; i++; } sum = sum + fact; n = n / 10; } if(sum == temp) printf("%d is a strong number
",temp); else printf("%d is not a strong number
",temp); return 0;}
输出当执行上述程序时,它产生以下结果 −
run 1:enter a number : 145145 is a strong numberrun 2:enter a number : 2525 is not a strong number
以上就是c程序用于判断给定的数字是否为强数的详细内容。