编写一个 C 程序,使用 for 循环检查该数字是否是克里希纳穆蒂数。首先,如果数字等于其各位数字的因子之和,则它是一个克里希纳穆蒂数。此 C 示例将数字分解为各位数字,并找到每个数字的因子之和。接下来,如果条件检查总和是否等于实际数字且为真,则它是一个克里希纳穆蒂数。
#include <stdio.h>
int main()
{
long fact;
int Number, tempNum, rem, Sum = 0, i;
printf("Enter Number to Check for Krishnamurthy Number = ");
scanf("%d", &Number);
for (tempNum = Number; tempNum > 0; tempNum = tempNum / 10)
{
fact = 1;
rem = tempNum % 10;
for (i = 1; i <= rem; i++)
{
fact = fact * i;
}
Sum = Sum + fact;
}
if (Number == Sum)
printf("\n%d is a Krishnamurthy Number.\n", Number);
else
printf("\n%d is not a Krishnamurthy Number.\n", Number);
}

此 C 程序使用 while 循环检查给定数字是否为克里希纳穆蒂数。
#include <stdio.h>
int main()
{
long fact;
int Number, tempNum, rem, Sum = 0, i;
printf("Enter Number to Check for Krishnamurthy Number = ");
scanf("%d", &Number);
tempNum = Number;
while (tempNum > 0)
{
fact = 1;
i = 1;
rem = tempNum % 10;
while (i <= rem)
{
fact = fact * i;
i++;
}
Sum = Sum + fact;
tempNum = tempNum / 10;
}
if (Number == Sum)
printf("\n%d is a Krishnamurthy Number.\n", Number);
else
printf("\n%d is not a Krishnamurthy Number.\n", Number);
}
Enter Number to Check for Krishnamurthy Number = 40585
40585 is a Krishnamurthy Number.
Enter Number to Check for Krishnamurthy Number = 2248
2248 is not a Krishnamurthy Number.
在此 C 示例中,Calculate_fact 返回数字的阶乘,Krishnamurthy_Number 将数字分解为各位数字并找到阶乘之和。最后,此 C 程序打印 1 到 N 或给定范围内的克里希纳穆蒂数。
#include <stdio.h>
long Calculate_fact(int Number)
{
if (Number == 0 || Number == 1)
return 1;
else
return Number * Calculate_fact(Number - 1);
}
int Krishnamurthy_Number(int Number)
{
int rem, Sum = 0;
long fact;
for (; Number > 0; Number = Number / 10)
{
fact = 1;
rem = Number % 10;
fact = Calculate_fact(rem);
Sum = Sum + fact;
}
return Sum;
}
int main()
{
int Number, Sum = 0, min, max;
printf("Please Enter the min & max Krishnamurthy Number = ");
scanf("%d %d", &min, &max);
printf("The List of Krishnamurthy Numbers\n");
for (Number = min; Number <= max; Number++)
{
Sum = Krishnamurthy_Number(Number);
if (Number == Sum)
{
printf("%d ", Number);
}
}
printf("\n");
}
Please Enter the min & max Krishnamurthy Number = 1 100000
The List of Krishnamurthy Numbers
1 2 145 40585