C程序判断给定数字是否为强数


强数是指其各位数字阶乘之和等于自身。

示例

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

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

输出

执行上述程序后,将产生以下结果:

Run 1:
Enter a number : 145
145 is a strong number
Run 2:
Enter a number : 25
25 is not a strong number

更新于:2023年11月6日

35K+ 次浏览

开启你的职业生涯

完成课程获得认证

开始学习
广告