本程式接收使用者輸入的一個三位數,驗證範圍後判斷是否為阿姆斯壯數(水仙花數),並給予相應提示訊息。
#include <stdio.h>
int main()
{
int num, a, b, c;
printf("請輸入一個三位數:");
scanf("%d", &num);
if(num < 100 || num > 999)
{
printf("%d不是三位數,輸入有誤!\n", num);
}
else
{
a = num % 10; // 個位
b = num / 10 % 10; // 十位
c = num / 100; // 百位
if(a*a*a + b*b*b + c*c*c == num)
{
printf("%d is Armstrong.\n", num);
}
else
{
printf("%d is not Armstrong!\n", num);
}
}
return 0;
}

| 輸入 | 輸出 | 說明 |
|---|---|---|
| 153 | 153 is Armstrong. | 1+125+27=153 ✓ |
| 370 | 370 is Armstrong. | 27+343+0=370 ✓ |
| 123 | 123 is not Armstrong! | 1+8+27=36 ≠ 123 |
| 99 | 99不是三位數,輸入有誤! | 範圍檢查生效 |
| 1000 | 1000不是三位數,輸入有誤! | 範圍檢查生效 |
long long 支援更大位數判斷。scanf 失敗處理(清緩衝區)。