Easy–medium · digits

Armstrong Number

153 is famous because 1³ + 5³ + 3³ = 153. Generalize: count the digits, then raise each to that power and sum. Digit Sum is the warm-up.

At a glanceAn n-digit number is an Armstrong number when its digits, each raised to the n-th power, sum back to itself (153 = 1³ + 5³ + 3³) — count the digits first, then loop again to add the powers.

Problem

Input: one integer N (1 ≤ N ≤ 10⁶).

Output: yes if N is an Armstrong number, else no.

Samples:

InputOutput
153yes
123no
9474yes

Your solution

main.cpp — your solution
Custom input for "Run my input"

Judging runs in your browser against hidden tests. A runaway loop is cut off after 3 seconds — still, make sure every loop has an exit. Your code is saved in this browser as you type.

Stuck? Hints

Hint 1 — two passes

First pass: copy n into a temp, divide by 10 until it's 0, counting how many digits d you saw. Second pass: extract each digit with % 10, raise it to the d-th power (a small inner loop), and add to a running sum.

Hint 2 — no pow() needed

JSCPP has no <cmath>, so compute the power with a loop (multiply digit by itself d times). Keep the running sum in long long — 999999 has 6 digits and 9⁶ × 6 can exceed 32-bit int.

Solution (try it first!)
#include <iostream>
using namespace std;

int main() {
    int n;
    cin >> n;
    int t = n, d = 0;
    while (t) { d++; t /= 10; }
    t = n;
    long long sum = 0;
    while (t) {
        int dig = t % 10;
        long long p = 1;
        for (int i = 0; i < d; i++) p *= dig;
        sum += p;
        t /= 10;
    }
    cout << (sum == n ? "yes" : "no") << endl;
    return 0;
}

Stretch: print every Armstrong number below 1000000, or handle the one-digit edge case explicitly (each one is). Related: Digit Sum, LCM.

Deep dive

Why this works

You need the digit count before you can compute anything, so the natural shape is two passes: one to measure, one to sum. Splitting a problem into "what do I need to know first" is the transferable skill.

The cost

O(d) time where d is the number of digits (at most 7 here), O(1) space. The inner power loop runs d times per digit, so d² total — still trivial.

Where it goes wrong

  • Reaching for pow(digit, d) from <cmath> — JSCPP does not ship it; write the small multiply loop instead.
  • Keeping the sum in int — 9⁶ × 6 = 3,188,646 still fits, but 7-digit inputs push past the int ceiling; long long costs nothing here.

Next step

Print every Armstrong number under a million (there are only 88) by looping the check. Least Common Multiple is the other half of the same digit-and-gcd toolbox.

Two things to remember

  • Count the digits first; the power depends on how many there are.
  • No <cmath> in the browser engine — a three-line loop replaces pow().
🐞 This version fails — open it and try

Always cubes the digits — right for 153, wrong for 9474 (four digits need the fourth power)

#include <iostream>
using namespace std;

int main() {
    int n;
    cin >> n;
    int t = n, sum = 0;
    while (t) {
        int d = t % 10;
        sum += d * d * d;   // hard-coded cube: only works for 3-digit numbers
        t /= 10;
    }
    cout << (sum == n ? "yes" : "no") << endl;
    return 0;
}