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:
#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.
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.
Enter a number: 153
153 is an 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:
- Read the integer
nfrom the user. - Initialize
sumto 0 andtempwithn. - Repeat while
tempis 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). - Check if
sumis equal ton. - 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 --> KHow 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, andremto store various states. - Prompt the user for input using
printfand read the number usingscanf. - Store the original number in
tempfor manipulation. - In the
whileloop, calculate the cube of each digit and add it tosum. - Remove the processed digit with
temp /= 10to continue the loop with the next digit. - After the loop, compare
sumto the originalnto determine if it's an Armstrong number. - Output the result with
printf.
Quick Quiz — test your understanding
Tap an option to check your answerWhat is an Armstrong number?
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.
Related Programs
If you found this program helpful, check out these related programs:
- C Program to Display Factors of a Number
- C Program to Check Prime or Armstrong Number Using User-defined Function
- C Program to Check Whether a Number is Prime or Not
- C Program to Display Prime Numbers Between Two Intervals
- C Program to Check if a Number is Sum of Two Primes
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.