What Is Armstrong Number: Definition and C Program

If you've been exploring C programming, chances are you've come across something called an Armstrong Number. Sounds fancy, right? But don’t worry, it’s simpler than it sounds, and it's a great example to learn about logic building, loops, and conditionals in C.

In this post, we’ll explore what an Armstrong number really is, how to check for one using a C program, and why this concept helps solidify your programming fundamentals.

What Is Armstrong Number

Want to practice such cool C programs with real-world application?


Explore Uncodemy’s C Programming Blog and get started today!

What Is an Armstrong Number?

An Armstrong Number (also known as a narcissistic number or pluperfect number) is a number that is equal to the sum of its own digits each raised to the power of the number of digits.

Formula:

For a n digit number, an Armstrong number satisfies:
abcd... = a^n + b^n + c^n + d^n + ...

Example:

  • 153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153 → Armstrong number
  • 9474 = 9^4 + 4^4 + 7^4 + 4^4 = 9474 → Armstrong number

Why Learn About Armstrong Numbers in C?

Because it teaches you:

  • Breaking a number into digits
  • Working with loops
  • Power operations
  • Condition checking

It’s an ideal beginner problem that requires critical thinking and structured logic.

C Program to Check Armstrong Number (3 Digits)

Copy Code

#include <stdio.h>

#include <math.h>

int main() {

    int num, originalNum, remainder, result = 0;

    printf("Enter a 3-digit number: ");

   scanf("%d", &num);

    originalNum = num;

    while (originalNum != 0) {

        remainder = originalNum % 10;

        result += remainder * remainder * remainder;

        originalNum /= 10;

    }

    if (result == num)

       printf("%d is an Armstrong number.\n", num);

    else

       printf("%d is not an Armstrong number.\n", num);



    return 0;

}

Sample Output:

Enter a 3-digit number: 371

371 is an Armstrong number.

How Does This Program Work?

Let’s break it down:

  • It asks the user for input.
  • Stores a copy of the number.
  • Extracts each digit using % 10
  • Raises the digit to the power of 3 (since it’s 3-digit)
  • Sums all the powered digits
  • Compares the result with the original number
  •  

Generalized C Program for Any Digit Armstrong Number

Copy Code

#include <stdio.h>

#include <math.h>

int main() {

    int num, originalNum, remainder, result = 0, n = 0;

    printf("Enter an integer: ");

   scanf("%d", &num);

    originalNum = num;

    while (originalNum != 0) {

        originalNum /= 10;

        ++n;

    }

    originalNum = num;

    while (originalNum != 0) {

        remainder = originalNum % 10;

        result += pow(remainder, n);

        originalNum /= 10;

    }

    if (result == num)

       printf("%d is an Armstrong number.\n", num);

    else

       printf("%d is not an Armstrong number.\n", num);

    return 0;

}

More Examples of Armstrong Numbers

  • 1-digit: Every number (0 to 9) is an Armstrong number
  • 3-digit: 153, 370, 371, 407
  • 4-digit: 1634, 8208, 9474

Real-World Analogy

Think of Armstrong numbers like a password that only opens a digital lock if the digits cooperate perfectly using a math trick. If even one digit misbehaves, the lock won’t open. It’s that precision that makes this number special.

FAQs on Armstrong Numbers in C

Q1. Can Armstrong numbers have more than 3 digits?
Yes! Numbers like 9474 and 54748 are valid Armstrong numbers.

Q2. How do you calculate the power in C?
Use the pow() function from <math.h>

Q3. What if I use ^ instead of pow()?
That would be wrong. In C, ^ is the bitwise XOR operator, not exponentiation.

Q4. What type should I use for large Armstrong numbers?
Use long long int or unsigned long for larger numbers.

Q5. Can we write this program without using math.h?
Yes, by multiplying the number manually (e.g., remainder * remainder * remainder)

Tips for Beginners

  • Always store the original number if you plan to modify it in the program
  • Use while loops for digit-based problems
  • Don’t forget to include math.h when using pow()

Conclusion

Armstrong numbers might seem like a mathematical riddle, but once decoded with C, they become an excellent exercise in logic building and number manipulation. From working with loops to practicing mathematical operations, this program has it all.

Want to build more programs like this from scratch?
Follow Uncodemy’s C Programming Tutorials and take your coding to the next level!

Meta Title:

What Is an Armstrong Number? Definition, Examples & C Program

Meta Description:

Understand what an Armstrong number is, its definition, and how to implement it in C programming. Includes detailed examples, C code, output, and explanation with Uncodemy's learning re

Placed Students

Our Clients

Partners

...

Uncodemy Learning Platform

Uncodemy Free Premium Features

Popular Courses