View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All

How to Check Armstrong number in C : From Theory to Implementation

Updated on 16/04/20256,676 Views

The Armstrong number in C is a widely used example in programming tutorials, computer science courses, and technical interviews. It provides a solid foundation for learning how to extract digits from numbers, use control structures like loops and conditionals, and apply mathematical functions. 

What makes this problem especially valuable is that it teaches how to break a problem down logically, develop a clean algorithm, and translate that logic into a working C program. As you progress, the same basic concept can be extended to support numbers of any length, and that’s why it’s taught in every software development course

In this complete guide, we’ll explore every angle of the Armstrong number in C—from understanding what it is, to building flowcharts, writing multiple versions of the program, creating optimized algorithms, and evaluating time and space complexity. If you’re looking to deepen your understanding of C programming and mathematical logic, this is the perfect place to start. 

What is the Armstrong Number in C?  

Before writing a program, it's important to grasp the concept of an Armstrong number. In simple terms, an Armstrong number is one that equals the sum of its own digits, each raised to the power of the number of digits. This idea forms the basis for the logic you'll later implement in C.

Key points to understand:

An Armstrong number is also known as a narcissistic number or pluperfect number.

The number 153 is an Armstrong number because: 

  • An Armstrong number is also known as a narcissistic number or pluperfect number.
  • The number 153 is an Armstrong number because: 1^3 + 5^3 + 3^3 = 153
  • The number 9474 is also an Armstrong number: 9^4 + 4^4 + 7^4 + 4^4 = 9474.
  • The number of digits in the number determines the power each digit is raised to.

If the final sum of powered digits equals the original number, then it qualifies as an Armstrong number.

Must Read: 29 C Programming Projects in 2025 for All Levels [Source Code Included]

Flowchart for Armstrong Number in C 

To better understand how to approach the Armstrong number problem, let’s break down the process using a flowchart. A flowchart is an excellent way to map out the steps involved in checking if a given number is an Armstrong number. It helps visualize the entire flow of logic before diving into the actual implementation in C.

Step-by-Step Breakdown of the Flowchart:

  1. Start the Program: The program begins execution from here.
  2. Input the Number: The user is prompted to enter a number that will be checked to see if it's an Armstrong number.
  3. Store the Original Number: The number inputted by the user is stored in a variable (let’s say number). This number will be used for comparison after calculating the sum of its digits raised to the power of the number of digits.
  4. Initialize the Temporary Variable for Digit Extraction: Copy the original number to a temporary variable (say temp) so that the original number remains unchanged during the digit extraction process.
  5. Count the Number of Digits: To determine the number of digits in the number, we divide it by 10 repeatedly. This can be done by using a loop that counts the number of times the number can be divided by 10 until it becomes 0. This count is stored in a variable, typically called n (for the number of digits).
  6. Reset the Number to the Original: After counting the digits, reset the variable temp back to the original number so that we can perform operations on the digits.
  7. Extract Each Digit and Calculate the Sum of Powers: Using a loop, extract each digit from the number. This is done by taking the modulus of the number by 10 (i.e., digit = temp % 10), which gives the last digit. Then, raise the digit to the power of n (the number of digits). The result is added to a running total, say sum.
  8. Check if the Sum Equals the Original Number: After the loop finishes (i.e., after processing all digits), compare the accumulated sum with the original number. If the sum equals the original number, then the number is an Armstrong number. Otherwise, it’s not.
  9. Output the Result: Based on the comparison, print either:
    1. "Armstrong number" if the sum equals the original number.
    2. "Not an Armstrong number" if the sum does not equal the original number.
  1. End the Program: The program finishes execution.

Pursue Post Graduate Diploma in Management 

This flowchart logically breaks down the process into clear, manageable steps that mirror the structure of a typical C program. By following this flow, you can ensure that your code is well-organized and efficient.

Now that we have a clear understanding of the flowchart and how we’ll approach the problem, let’s implement it in C. Below is the C program that checks if a given three-digit number is an Armstrong number.

Armstrong Number in C for Three-Digit Number 

The program uses a simple approach where we directly compute the sum of the cubes of the digits and check if the result is equal to the original number. For this, you’ve to understand the if-else statement in C

C Program for Three-Digit Armstrong Number

#include <stdio.h>

int main() {
int number, originalNumber, remainder, result = 0;

// Input number from user
printf("Enter a three-digit number: ");
scanf("%d", &number);

originalNumber = number; // Store the original number to compare later

// Process each digit of the number
while (originalNumber != 0) {
remainder = originalNumber % 10; // Extract the last digit
result += remainder * remainder * remainder; // Add the cube of the digit
originalNumber /= 10; // Remove the last digit
}

// Check if the result equals the original number
if (result == number)
printf("%d is an Armstrong number.\n", number);
else
printf("%d is not an Armstrong number.\n", number);

return 0;
}

Sample Output:

Enter a three-digit number: 153
153 is an Armstrong number.

Explanation:

  1. Input the Number: We start by taking input from the user using scanf and storing it in the number variable.
  2. Store the Original Number: The original number is stored in a separate variable originalNumber so that we can compare it with the result later.
  3. Extract and Process Digits: The program uses a while loop to extract each digit from the number. In each iteration, we use number % 10 to get the last digit, and number /= 10 to remove that last digit from the number.
  4. Calculate the Cube of Each Digit: We raise each digit to the power of three (since we’re working with three-digit numbers) and add the result to the result variable.
  5. Compare the Result: After processing all the digits, we compare the accumulated sum with the original number. If the sum matches, we print that the number is an Armstrong number; otherwise, it’s not.

This program works perfectly for three-digit numbers and serves as a good starting point for understanding how to check Armstrong numbers in C.

In addition, learn about the structure of a C program to efficiently build an Armstrong number in C program. 

Algorithm for Armstrong Number (for N-digit Numbers) 

Now that we've seen how to check Armstrong numbers for a fixed number of digits (in this case, three digits), let's generalize the solution to handle Armstrong numbers for N-digit numbers. This allows the program to work for any number of digits, not just three-digit numbers.

Also learn about input and output functions to understand the example code and algorithm better. 

Algorithm for Armstrong Number (for N-digit Numbers):

  1. Start: Begin the program execution.
  2. Input the Number: Take the input number from the user.
  3. Calculate the Number of Digits
  • Initialize a variable n to store the number of digits in the number.
  • Use a loop to count the digits of the number by repeatedly dividing the number by 10 until it becomes 0.
  1. Store the Original Number: Store the original number in a variable originalNumber for later comparison.
  2. Initialize Variables for the Calculation: Initialize a variable sum = 0 to accumulate the sum of the digits raised to the power of n.
  3. Extract Each Digit
  • Extract the digits of the number by using the modulus operation (temp % 10), where temp is a copy of the original number.
  • Raise each digit to the power of n and add it to the sum variable.
  • Remove the last digit from temp by performing integer division (temp /= 10).
  1. Compare the Sum with the Original Number
  • After processing all digits, compare the calculated sum with the original number.
  • If sum equals the original number, then it's an Armstrong number.
  • Otherwise, it's not an Armstrong number.
  1. Output the Result: Print the result based on the comparison.
  2. End: End the program execution.

This algorithm is designed to be flexible for any number of digits and can be implemented to check Armstrong numbers for numbers of any size.

Must Explore: Introduction to C Tutorial

Armstrong Number Program in C for N-digit Numbers

Now that we have the general algorithm for checking Armstrong numbers for N-digit numbers, let’s implement this in C. The program will be able to handle any number of digits, not just three-digit numbers. We’ll compute the sum of each digit raised to the power of the total number of digits and check if it equals the original number.

C Program for N-digit Armstrong Number

#include <stdio.h>
#include <math.h>

int main() {
int number, originalNumber, remainder, result = 0, n = 0;

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

originalNumber = number; // Store the original number to compare later

// Count the number of digits in the number
while (originalNumber != 0) {
originalNumber /= 10; // Remove the last digit
++n; // Increment the digit count
}

originalNumber = number; // Reset the original number to perform calculations

// Process each digit and calculate the sum of powers
while (originalNumber != 0) {
remainder = originalNumber % 10; // Extract the last digit
result += pow(remainder, n); // Add the digit raised to the power of n
originalNumber /= 10; // Remove the last digit
}

// Check if the result equals the original number
if (result == number)
printf("%d is an Armstrong number.\n", number);
else
printf("%d is not an Armstrong number.\n", number);

return 0;
}

Sample Output:

Enter a number: 153

153 is an Armstrong number.

Explanation:

1. Input the Number: We prompt the user to enter a number using `scanf`.

2. Count the Number of Digits: The program uses a `while` loop to count the number of digits in the number. For each iteration, the number is divided by 10, and the digit count `n` is incremented.

3. Store the Original Number: The original number is stored in `originalNumber` so we can compare it later after calculating the sum.

4. Calculate the Sum of Powers of the Digits: We use another `while` loop to process each digit. The last digit is extracted using `originalNumber % 10`, and then raised to the power of `n` using the `pow()` function. The result is added to `result`.

5. Compare the Result: After processing all the digits, the sum (`result`) is compared with the original number. If they match, the number is an Armstrong number; otherwise, it’s not.

This program works for any number of digits and will accurately check if the given number is an Armstrong number.

Analysis of Algorithm

Now that we’ve written the program for Armstrong numbers, it’s important to analyze the algorithm’s efficiency. Understanding the time and space complexity of your solution helps to determine how it will scale as input sizes grow and whether it's optimal for large inputs.

Time Complexity

Counting the Digits: In the first part of the program, we count the number of digits in the input number. The loop runs until the number becomes 0. If the number has *n* digits, this loop will execute *n* times. This step has a time complexity of O(n).

Processing Each Digit: After counting the digits, the program again loops through each digit of the number. In each iteration, the program extracts a digit and raises it to the power of *n*. Since there are *n* digits in the number, this loop also runs *n* times, resulting in a time complexity of O(n).

Total Time Complexity: Both loops are linear with respect to the number of digits in the number. Therefore, the overall time complexity of the algorithm is **O(n)**, where *n* is the number of digits in the input number.

Space Complexity

The space complexity is determined by the amount of memory required to store the variables. The program uses a constant amount of extra space: for the number, temporary variables, and the result. No additional space is used that grows with input size. Therefore, the space complexity is **O(1)**.

Performance Consideration

Time Complexity Considerations: The algorithm works efficiently for smaller numbers with relatively fewer digits, as the time complexity grows linearly with the number of digits. However, as the number of digits increases, especially for very large numbers, the number of operations (especially the power function) can significantly affect performance. For very large numbers, this may cause slower execution times.

Optimization: If performance for large numbers becomes a concern, certain optimizations could be considered, such as improving the digit extraction process or exploring mathematical properties that might reduce the number of computations. However, for typical use cases (with numbers having a few digits), this algorithm is efficient enough.

In conclusion, the program is both time-efficient (O(n)) and space-efficient (O(1)) for typical inputs, but it may need optimizations for handling very large numbers.

Real-World Applications of Armstrong Numbers

While Armstrong numbers may seem like a purely theoretical concept, they have interesting applications in fields such as cryptography, digital fingerprinting, and educational programming. Here are a few areas where the concept or similar calculations might be used:

Cryptography and Security

Armstrong numbers share a mathematical property that makes them interesting in cryptographic algorithms, particularly in the creation of complex encryption keys or digital signatures. Though Armstrong numbers themselves aren’t used directly in encryption, similar concepts of number manipulation and digit operations can be part of more advanced cryptographic functions.

Hashing and Digital Fingerprinting

In data security, hashing functions often use operations involving numbers raised to certain powers. The idea of an Armstrong number, which combines digit extraction and mathematical operations, can inspire efficient ways of creating digital fingerprints (hashes) for files or messages. These fingerprints are used for quick comparisons or integrity checks, making Armstrong-like operations useful in creating robust hashing algorithms.

Educational Programming and Algorithmic Problem Solving

Armstrong numbers provide an excellent exercise for learning loops, recursion, and mathematical functions in programming. By implementing Armstrong number checks, students can grasp core programming concepts while developing problem-solving skills. These types of exercises are commonly used in coding challenges and competitive programming.

Data Integrity and Error Checking

In some systems, mathematical properties like Armstrong numbers can be used for error detection and correction. For instance, the sum of powered digits could potentially serve as a check value in a data transmission protocol. While not common, similar methods have been explored in contexts where data integrity is critical, and checksums need to be calculated to ensure accurate data transfer.

Although Armstrong numbers are not widely applied directly in day-to-day real-world technology, the techniques of manipulating digits and performing mathematical operations can be found across several fields, especially in areas that require secure data manipulation or mathematical verification.

Conclusion 

In this guide, we've explored Armstrong numbers and how to implement a solution in C. We began by understanding what an Armstrong number is one where the sum of its digits raised to the power of the number of digits equals the number itself. 

Through a flowchart, we mapped out the steps involved in checking for Armstrong numbers, followed by writing C programs to handle both three-digit and N-digit numbers. The approach included calculating the sum of powered digits and comparing it with the original number, which led to clear and efficient solutions.

In the analysis of the algorithm, we noted that the program runs with a time complexity of O(n), where n is the number of digits, and a space complexity of O(1). While the program performs well for smaller numbers, it may need optimization for very large inputs. Overall, this exercise serves as a great way to learn about loops, mathematical functions, and algorithm efficiency in C. It’s a useful skill set for both beginners and experienced programmers alike.

FAQs 

1. What is an Armstrong Number?  

An Armstrong number is a number that is equal to the sum of its own digits each raised to the power of the number of digits. For example, 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153.

2. How do you check if a number is an Armstrong number in C?  

To check if a number is an Armstrong number in C, you need to extract each digit, raise it to the power of the number of digits, and sum the results. If the sum is equal to the original number, then it's an Armstrong number.

3. What is the difference between an Armstrong number and a perfect number?  

An Armstrong number is the sum of its digits raised to the power of the number of digits, while a perfect number is a number that is equal to the sum of its proper divisors (excluding the number itself).

4. Is 153 an Armstrong number?  

Yes, 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153.

5. How do I calculate the number of digits in a number?  

To calculate the number of digits in a number, divide the number by 10 repeatedly until it becomes 0, while counting the number of divisions. Alternatively, you can use logarithms to calculate the number of digits.

6. Can Armstrong numbers be negative?  

No, Armstrong numbers are typically considered only for positive integers. Negative numbers do not have a meaningful interpretation in this context.

7. What is the largest Armstrong number?  

There is no definitive "largest" Armstrong number because the set of Armstrong numbers is infinite. However, as numbers grow larger, the calculations become more complex and less practical for checking in real-world applications.

8. Are Armstrong numbers useful in real-world applications?  

While Armstrong numbers themselves are not used directly in most applications, the underlying mathematical techniques for digit manipulation are employed in cryptography, error checking, and educational programming.

9. Can Armstrong numbers be fractional?  

No, Armstrong numbers are defined for integers only. Fractions or decimals are not considered Armstrong numbers.

10. How is the time complexity of checking an Armstrong number calculated?  

The time complexity is O(n), where n is the number of digits in the number. This is because we need to extract each digit and perform a mathematical operation (raising it to the power of n) for each digit.

11. What are some examples of Armstrong numbers?  

Some examples of Armstrong numbers include 153, 370, 371, and 407. These are all three-digit Armstrong numbers. Larger Armstrong numbers include 9474, 93084, and 548834.

image

Take a Free C Programming Quiz

Answer quick questions and assess your C programming knowledge

right-top-arrow
image
Join 10M+ Learners & Transform Your Career
Learn on a personalised AI-powered platform that offers best-in-class content, live sessions & mentorship from leading industry experts.
advertise-arrow

Free Courses

Start Learning For Free

Explore Our Free Software Tutorials and Elevate your Career.

upGrad Learner Support

Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)

text

Indian Nationals

1800 210 2020

text

Foreign Nationals

+918068792934

Disclaimer

1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.

2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.