✓ Link copied to clipboard
C 📅 July 6, 2026 ⏱ 5 min read

C Program to Check Armstrong Number

C Program to Check Armstrong Number

Armstrong numbers are pretty cool. They’re numbers that equal the sum of their own digits raised to the power of how many digits they have. For instance, take 153. It’s an Armstrong number since 13 + 53 + 33 is 153. Let’s dive into how to check if a number is an Armstrong number in C.

Prerequisites for the C Program to Check Armstrong Number

Before you proceed with writing this program, ensure you understand the following:

  • Concept of loops, specifically while loops.
  • Use of the % and / operators in C for modulus and division.
  • Basic arithmetic operations and integer variables in C.
  • <stdio.h> for input and output functions.
  • Any C compiler such as GCC 9 or newer — or just run it in the EduSeekho online compiler.

Source Code of C Program to Check Armstrong Number

Here's the complete C code to determine if a number is Armstrong:

armstrong.cC▶ Run
#include <stdio.h>

int main() {
    int n, sum = 0, temp, rem;

    // Read the number from the user
    printf("Enter a number: ");
    scanf("%d", &n);

    temp = n;

    // Calculate the sum of the cubes of its digits
    while (temp != 0) {
        rem = temp % 10;
        sum += rem * rem * rem;
        temp /= 10;
    }

    // Check if the sum is equal to the original number
    if (sum == n) {
        printf("%d is an Armstrong number.\n", n);
    } else {
        printf("%d is not an Armstrong number.\n", n);
    }

    return 0;
}

Try this code yourself — free

Run it in the EduSeekho online compiler. No setup needed.

▶ Open Compiler

Output of the Armstrong Number Program

When you run the program and input a number like 153, it shows whether the number is an Armstrong number.

Outputtext
Enter a number: 153
153 is an Armstrong number.
Output of C Program to Check Armstrong Number
Output of C Program to Check Armstrong Number

Algorithm to Check Armstrong Number in C

Reversing a number means processing its digits from last to first. Here's the logic the program follows, step by step:

  1. Read the integer n from the user.
  2. Initialize sum to 0 and temp with n.
  3. Repeat while temp is not zero:
    • Extract the last digit of temp (using modulus 10).
    • Add the cube of the digit to sum.
    • Remove the last digit from temp (using integer division by 10).
  4. Check if sum is equal to n.
  5. If true, it's an Armstrong number; otherwise, it's not.
flowchart TD
  A([Start]) --> B[/Read n/]
  B --> C[sum = 0, temp = n]
  C --> D{"temp != 0?"}
  D -->|Yes| E["rem = temp %% 10"]
  E --> F["sum += rem^3"]
  F --> G["temp /= 10"]
  G --> D
  D -->|No| H{"sum == n?"}
  H -->|Yes| I([Print "Armstrong number"]) 
  H -->|No| J([Print "Not an Armstrong number"]) 
  I --> K([End])
  J --> K
Flowchart: the algorithm step by step

How the C Program to Check Armstrong Number Works

Let's break down how this program checks if a number is an Armstrong number.

  • #include <stdio.h>: Includes standard input and output library for necessary functions.
  • Declare variables: n, sum, temp, and rem to store various states.
  • Prompt the user for input using printf and read the number using scanf.
  • Store the original number in temp for manipulation.
  • In the while loop, calculate the cube of each digit and add it to sum.
  • Remove the processed digit with temp /= 10 to continue the loop with the next digit.
  • After the loop, compare sum to the original n to determine if it's an Armstrong number.
  • Output the result with printf.
🧠

Quick Quiz — test your understanding

Tap an option to check your answer

What is an Armstrong number?

🎉Correct! An Armstrong number is the sum of cubes of its digits equal to the number itself.
🤔Not quite. Think about the special relationship between the digits and the number itself.

Time and Space Complexity of the C Program to Check Armstrong Number

Understanding the complexity of this program helps evaluate its efficiency.

Time Complexity: O(d), where d is the number of digits in the number. Each digit is processed once in the loop, as seen in while (temp != 0). For example, 153 (3 digits) means about 3 iterations.

Space Complexity: O(1). The program uses a fixed set of integer variables: n, sum, temp, and rem. These do not depend on the size of the input number.

⚠️

Common mistake: Not resetting temp to n before the loop gives wrong results.

Key takeaway: An Armstrong number is a unique number where the sum of its digits' cubes adds up to itself.

FAQs About the C Program to Check Armstrong Number

Here are answers to some common questions about this program.

An Armstrong number equals the sum of its digits raised to the power of the number of digits.
No, Armstrong numbers are for non-negative integers only.
The program checks each digit one by one, so it's flexible with any number of digits.
No, it only works with integer inputs.
153 is an Armstrong number because 13 + 53 + 33 = 153.
Zero counts as an Armstrong number since 01 = 0.

If you found this program helpful, check out these related programs:

Conclusion

Checking for Armstrong numbers is a fun way to get your head around digit manipulation. Try messing with bigger numbers or even coding something that checks for four-digit Armstrong numbers. It’s useful for programming practice and interviews too.

Keep at it with EduSeekho's C programming quiz or whip up your own programs in our online compiler.