
一个强数是一个数字,其中各位数字的阶乘之和等于该数字本身。
=1+2+6 =9
在这个例子中,123不是一个强数,因为各位数字的阶乘之和不等于该数字本身。
=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</p><p>",temp);
else
printf("%d is not a strong number</p><p>",temp);以下是用于判断给定数字是否为强数的C程序:
在线演示
#include<stdio.h>
int main(){
int n,i;
int fact,rem;
printf("</p><p>Enter a number : ");
scanf("%d",&n);
printf("</p><p>");
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</p><p>",temp);
else
printf("%d is not a strong number</p><p>",temp);
return 0;
}当执行上述程序时,它产生以下结果 −
Run 1: Enter a number : 145 145 is a strong number Run 2: Enter a number : 25 25 is not a strong number
以上就是C程序用于判断给定的数字是否为强数的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号