C 語言如何檢查阿姆斯特朗數?
如果一個數的各位數字的立方和等於該數本身,那麼這個數被稱為阿姆斯特朗數。它是一個數學概念,通常用於程式設計中建立程式設計師的基本邏輯
Input:370 Output:370 is an Armstrong Number
說明
370 = 3*3*3 + 7*7*7 + 0*0*0 = 27 + 343 + 0 = 370
示例
include <iostream>
using namespace std;
int main() {
int n, num, rem, sum = 0;
cin >> n;
num = n;
while(num != 0) {
digit = num % 10;
sum += digit * digit * digit;
num /= 10;
}
if(sum == n)
printf("%d is an Armstrong number.", n );
else
printf("%d is not an Armstrong number.",n);
return 0;
}
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP