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;
}

更新於: 2019 年 8 月 19 日

347 次瀏覽

開啟您的 事業

完成課程即可獲得認證

開始
廣告
© . All rights reserved.