Beginner Number Problems C

Armstrong Number Program in C with Explanation

Check whether a number equals the sum of its digits each raised to the number of digits.

  • TimeO(d^2) per number with the simple power loop,
  • SpaceO(1)
  • Hints3
  • Read3 min
Quick answer

An Armstrong number equals the sum of its digits, each raised to the power of the number of digits. 153 is one because 1^3 + 5^3 + 3^3 = 153. In C, count the digits, then take each digit with n % 10, add digit^k to a sum, remove it with n / 10, and compare the sum with the original number. Between 1 and 1000 the Armstrong numbers are 1 to 9, 153, 370, 371 and 407.

Digit extraction with % and /Counting digitsInteger power with a loopFunctions in C
1

Problem statement

Write an Armstrong number program in C. Given a positive integer n, print whether it is an Armstrong number.

A number with k digits is an Armstrong number if the sum of each digit raised to the power k equals the number. Start with the 3-digit version that labs usually ask for, then write one that works for any number of digits.

Input

A positive integer n.

Output

"n is an Armstrong number" or "n is not an Armstrong number".

Examples

Example 1
Inputn = 153
Output153 is an Armstrong number

1 + 125 + 27 = 153.

Example 2
Inputn = 9474
Output9474 is an Armstrong number

4 digits, so use power 4: 6561 + 256 + 2401 + 256 = 9474.

Example 3
Inputn = 123
Output123 is not an Armstrong number

1 + 8 + 27 = 36, which is not 123.

Constraints

  • 1 <= n <= 99999999. For 9 or more digits the sum can pass the int limit, so use long long
2

Think before you code

Reveal the questions to ask yourself first
  • How do you take out the digits of a number one by one?
  • What power should each digit be raised to?
  • The loop destroys n. What do you compare with at the end?
3

Hints

Open one at a time — try again after each.
1 Hint 1
Copy n into temp and work on temp, so n stays unchanged for the final check.
2 Hint 2
Count digits first with a loop that divides by 10.
3 Hint 3
Write your own power loop with ints. pow() returns a double and can give rounding errors.
4

Approach

Reveal the step-by-step approach

Armstrong number logic

Number Digits k Calculation Sum Armstrong?
153 3 1³ + 5³ + 3³ 153 Yes
370 3 3³ + 7³ + 0³ 370 Yes
123 3 1³ + 2³ + 3³ 36 No
9474 4 9⁴ + 4⁴ + 7⁴ + 4⁴ 9474 Yes

Steps

  1. Count the digits k.
  2. Copy n into temp, set sum = 0.
  3. While temp > 0: d = temp % 10, add d to the power k to sum, temp = temp / 10.
  4. If sum == n, it is an Armstrong number.

The digit loop is the same one used for sum of digits and reverse a number.

5

Dry run

Walk through the example step by step

n = 153, k = 3, temp = 153, sum = 0.

temp | d = temp % 10 | d^3 | sum | temp / 10
-----+---------------+-----+-----+----------
153  | 3             | 27  | 27  | 15
15   | 5             | 125 | 152 | 1
1    | 1             | 1   | 153 | 0

sum == n, so 153 is an Armstrong number.

6

Solution

Reveal the full C solution
solution.c
#include <stdio.h>

int countDigits(int n) {
    int count = 0;
    do {
        count++;
        n /= 10;
    } while (n > 0);
    return count;
}

int power(int base, int exp) {
    int result = 1;
    for (int i = 0; i < exp; i++) {
        result *= base;
    }
    return result;
}

int isArmstrong(int n) {
    int k = countDigits(n);
    int temp = n, sum = 0;
    while (temp > 0) {
        sum += power(temp % 10, k);
        temp /= 10;
    }
    return sum == n;
}

int main(void) {
    int tests[] = {153, 370, 9474, 123};
    for (int i = 0; i < 4; i++) {
        if (isArmstrong(tests[i])) {
            printf("%d is an Armstrong number\n", tests[i]);
        } else {
            printf("%d is not an Armstrong number\n", tests[i]);
        }
    }

    printf("Armstrong numbers from 1 to 1000: ");
    for (int n = 1; n <= 1000; n++) {
        if (isArmstrong(n)) {
            printf("%d ", n);
        }
    }
    printf("\n");
    return 0;
}

Output:

153 is an Armstrong number
370 is an Armstrong number
9474 is an Armstrong number
123 is not an Armstrong number
Armstrong numbers from 1 to 1000: 1 2 3 4 5 6 7 8 9 153 370 371 407
  • countDigits uses do-while so that it returns 1 for n = 0 as well.
  • power multiplies in a loop and stays in integers.
  • Every single-digit number is an Armstrong number, because d to the power 1 is d.

Armstrong number in C for 3 digits (lab version)

Many lab manuals ask only for 3-digit numbers, so the power is fixed at 3:

#include <stdio.h>

int main(void) {
    int n, temp, sum = 0;
    printf("Enter a 3-digit number: ");
    scanf("%d", &n);

    temp = n;
    while (temp > 0) {
        int d = temp % 10;
        sum = sum + d * d * d;
        temp = temp / 10;
    }

    if (sum == n) {
        printf("%d is an Armstrong number\n", n);
    } else {
        printf("%d is not an Armstrong number\n", n);
    }
    return 0;
}
Enter a 3-digit number: 153
153 is an Armstrong number
Enter a 3-digit number: 123
123 is not an Armstrong number

This version is correct only for 3-digit input. For 1634 it would use cubes and say "not Armstrong", even though 1634 is one with power 4.

Armstrong numbers in a range

Range Armstrong numbers
1 to 9 1, 2, 3, 4, 5, 6, 7, 8, 9
100 to 999 153, 370, 371, 407
1000 to 9999 1634, 8208, 9474

There are no 2-digit Armstrong numbers.

Why not use pow()?

pow() from math.h works with double. On some older compilers, a result like pow(5, 3) can come out as 124.999..., which becomes 124 when stored in an int. A small int loop avoids that. With gcc you also need -lm to link math.h on many systems.

The Java version is on check whether a number is an Armstrong number. For more C practice, see loop programs in C.

TimeO(d^2) per number with the simple power loop, where d is the number of digits
SpaceO(1)
7

Review before the interview

Common mistakes

  • Comparing sum with temp after the loop. temp is 0 by then; compare with n.
  • Always cubing the digits. That only works for 3-digit numbers.
  • Forgetting to reset sum to 0 for each number when checking a range.
  • Using pow() and storing the double in an int, which can round down.

Edge cases to test

  • n = 0: the while loop does not run, sum is 0, so 0 counts as an Armstrong number. Some teachers start from 1.
  • Single-digit numbers are all Armstrong numbers.
  • Numbers with a 0 digit, such as 370 and 407, still work because 0 to any power is 0.
  • Negative numbers are not Armstrong numbers in the usual definition.

Interview follow-ups

  • Can you print all Armstrong numbers between two numbers entered by the user?
  • How would you make the power calculation faster by storing powers of 0 to 9?
  • What is the difference between an Armstrong number and a strong number?

Keep practising

Building your programming basics?

CodeBegun’s programs build problem-solving with mentor review and mock interviews, online or in Madhapur, Hyderabad.

Chat with us