Easy+ · digit logic

Armstrong Number

153 = 1³ + 5³ + 3³ — a number that equals the sum of its digits each raised to the power of how many digits it has. For three-digit numbers that is cubes.

Problem

Input: one integer n (0 ≤ n ≤ 999).

Output: Yes if n equals the sum of its digits cubed, No otherwise.

Sample:

InputOutput
153Yes
100No

Your solution

Program.cs — your solution
Custom input for "Run my input"

Judging runs entirely in your browser against hidden tests. Your code is saved locally as you type. Ctrl+Enter judges.

Stuck? Hints

Hint 1 — peel and cube

Same loop as Digit Sum, but add digit * digit * digit instead of digit. Keep the original value for the final comparison.

Hint 2 — the known answers

The three-digit Armstrong numbers are 153, 370, 371 and 407. If your code disagrees with those four, the bug is in the accumulation, not the comparison.

Solution (try first!)
int n = int.Parse(Console.ReadLine());
int orig = n, sum = 0;
while (n > 0)
{
    int digit = n % 10;
    sum += digit * digit * digit;
    n = n / 10;
}
Console.WriteLine(sum == orig ? "Yes" : "No");

Note the careful orig copy — the loop destroys n, exactly like in Palindrome Number. Next: Matrix Diagonal Sum.