编写一个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\n",temp);
else
printf("%d is not a strong number\n",temp);示例
以下是C程序,以查找给定的数字是否强-
#include<stdio.h>
int main(){
int n,i;
int fact,rem;
printf("\nEnter a number : ");
scanf("%d",&n);
printf("\n");
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\n",temp);
else
printf("%d is not a strong number\n",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
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下