For working professionals
For fresh graduates
More
5. Array in C
13. Boolean in C
18. Operators in C
33. Comments in C
38. Constants in C
41. Data Types in C
49. Double In C
58. For Loop in C
60. Functions in C
70. Identifiers in C
81. Linked list in C
83. Macros in C
86. Nested Loop in C
97. Pseudo-Code In C
100. Recursion in C
103. Square Root in C
104. Stack in C
106. Static function in C
107. Stdio.h in C
108. Storage Classes in C
109. strcat() in C
110. Strcmp in C
111. Strcpy in C
114. String Length in C
115. String Pointer in C
116. strlen() in C
117. Structures in C
119. Switch Case in C
120. C Ternary Operator
121. Tokens in C
125. Type Casting in C
126. Types of Error in C
127. Unary Operator in C
128. Use of C Language
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.
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:
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]
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:
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.
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.
#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:
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.
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):
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
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.
#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.
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.
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.
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)**.
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.
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.
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.
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.
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.
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).
Yes, 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153.
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.
No, Armstrong numbers are typically considered only for positive integers. Negative numbers do not have a meaningful interpretation in this context.
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.
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.
No, Armstrong numbers are defined for integers only. Fractions or decimals are not considered Armstrong numbers.
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.
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.
Take a Free C Programming Quiz
Answer quick questions and assess your C programming knowledge
Author
Start Learning For Free
Explore Our Free Software Tutorials and Elevate your Career.
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918068792934
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.