打印阿姆斯特朗数的 C 程序
2021-07-06 22:23:08
深夜i
--
--
打
印
阿
姆
斯
特
朗
数
的
序
C 程序打印阿姆斯壮数,在程序中,用户输入两个整数,我们打印整数之间的所有阿姆斯壮数。 使用 for 循环,我们检查这个范围内的数字。 在循环中,我们调用函数 check_armstrong,如果数字是 Armstrong,则返回“1”,否则返回“0”。 如果您不熟悉阿姆斯壮数,请参阅 C 中的阿姆斯壮数。
C程序
#include <stdio.h>
int check_armstrong(int);
int power(int, int);
int main ()
{
int c, a, b;
printf("Input two integers\n");
scanf("%d%d", &a, &b);
for (c = a; c <= b; c++)
if (check_armstrong(c) == 1)
printf("%d\n", c);
return 0;
}
int check_armstrong(int n) {
long long sum = 0, t;
int remainder, digits = 0;
t = n;
while (t != 0) {
digits++;
t = t/10;
}
t = n;
while (t != 0) {
remainder = t%10;
sum = sum + power(remainder, digits);
t = t/10;
}
if (n == sum)
return 1;
else
return 0;
}
int power(int n, int r) {
int c, p = 1;
for (c = 1; c <= r; c++)
p = p*n;
return p;
}
程序的输出(在示例输出中,我们打印范围为 [0, 1000000] 的 Armstrong 数字):
下载阿姆斯壮数字程序。
上一篇:
idea打包java可执行jar包
下一篇:
C 中的斐波那契数列
评论区