原文: https://beginnersbook.com/2014/06/c-program-to-check-armstrong-number/
如果数字的各位的立方和等于数字本身,则将数字称为阿姆斯特朗数。在下面的 C 程序中,我们检查输入的数字是否是阿姆斯特朗数。
#include<stdio.h>int main(){int num,copy_of_num,sum=0,rem;//Store input number in variable numprintf("\nEnter a number:");scanf("%d",&num);/* Value of variable num would change in thebelow while loop so we are storing it inanother variable to compare the resultsat the end of program*/copy_of_num = num;/* We are adding cubes of every digit* and storing the sum in variable sum*/while (num != 0){rem = num % 10;sum = sum + (rem*rem*rem);num = num / 10;}/* If sum of cubes of every digit is equal to number* itself then the number is Armstrong*/if(copy_of_num == sum)printf("\n%d is an Armstrong Number",copy_of_num);elseprintf("\n%d is not an Armstrong Number",copy_of_num);return(0);}
输出:
Enter a number: 370370 is an Armstrong Number
您可以像这样验证结果:
370 = 3*3*3 + 7*7*7 + 0*0*0= 27 + 343 + 0= 370
如您所见,数字 370 的各位立方和等于数字本身。
