原文: https://beginnersbook.com/2014/06/c-program-to-check-armstrong-number/

    如果数字的各位的立方和等于数字本身,则将数字称为阿姆斯特朗数。在下面的 C 程序中,我们检查输入的数字是否是阿姆斯特朗数。

    1. #include<stdio.h>
    2. int main()
    3. {
    4. int num,copy_of_num,sum=0,rem;
    5. //Store input number in variable num
    6. printf("\nEnter a number:");
    7. scanf("%d",&num);
    8. /* Value of variable num would change in the
    9. below while loop so we are storing it in
    10. another variable to compare the results
    11. at the end of program
    12. */
    13. copy_of_num = num;
    14. /* We are adding cubes of every digit
    15. * and storing the sum in variable sum
    16. */
    17. while (num != 0)
    18. {
    19. rem = num % 10;
    20. sum = sum + (rem*rem*rem);
    21. num = num / 10;
    22. }
    23. /* If sum of cubes of every digit is equal to number
    24. * itself then the number is Armstrong
    25. */
    26. if(copy_of_num == sum)
    27. printf("\n%d is an Armstrong Number",copy_of_num);
    28. else
    29. printf("\n%d is not an Armstrong Number",copy_of_num);
    30. return(0);
    31. }

    输出:

    1. Enter a number: 370
    2. 370 is an Armstrong Number

    您可以像这样验证结果:

    1. 370 = 3*3*3 + 7*7*7 + 0*0*0
    2. = 27 + 343 + 0
    3. = 370

    如您所见,数字 370 的各位立方和等于数字本身。