Introduction
An Armstrong number (also known as a narcissistic number, pluperfect digit, pluperfect number, or pluperfect digital invariant) in a given number base is a number that is the sum of its own digits raised to the power of the number of digits. C, a classic and foundational programming language, provides a straightforward approach to identifying such numbers. In this article, we will explore how to determine if a number is an Armstrong number using the C programming language.
What is an Armstrong Number?
An Armstrong number of a given number of digits is an integer such that the sum of its own digits raised to the power of the number of digits is equal to the number itself.
For example:
- 153 is an Armstrong number because: 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153
C Implementation
Algorithm:
- Take a number as input.
- Calculate the number of digits in the number.
- For each digit, raise it to the power of the total number of digits and calculate the sum.
- Compare the calculated sum with the original number. If they are the same, it’s an Armstrong number.
Code Example:
#include <stdio.h>
#include <math.h>
int main() {
int num, temp, sum = 0, n = 0;
printf("Enter a number: ");
scanf("%d", &num);
temp = num;
// Calculate number of digits
while(temp != 0) {
temp /= 10;
++n;
}
temp = num;
// Calculate sum of nth power of each digit
while(temp != 0) {
sum += pow(temp % 10, n);
temp /= 10;
}
if(sum == num)
printf("%d is an Armstrong number\n", num);
else
printf("%d is not an Armstrong number\n", num);
return 0;
}
Here, two libraries are included:
stdio.h
: This is the standard input-output library in C. It provides functions to read and write data. In our code, it is used for printing to the console and reading user input.math.h
: This library provides mathematical functions. In our code, it is used for thepow()
function which calculates the power of a number.
num
– Stores the number input by the user.temp
– Temporary variable used to manipulate and check the value ofnum
without altering it.sum
– Stores the sum of the nth power of each digit of the number.n
– Counts the number of digits in the number.
This loop runs until temp
becomes 0. In each iteration, temp
is divided by 10 (which effectively removes its last digit), and the count n
is incremented. This gives us the total number of digits in the number.
Here, we reset the value of temp
to num
to go through its digits again. For each digit (retrieved using temp % 10
), we raise it to the power of n
using pow()
and add it to the sum
. Then, we remove the last digit of temp
by dividing it by 10. This loop continues until all digits of temp
are processed.
We compare the sum
to the original num
. If they are equal, the number is an Armstrong number. Otherwise, it’s not The return 0;
statement signifies successful termination of the program.
Conclusion
Armstrong numbers are a fascinating mathematical concept, and their implementation in C helps novice programmers understand loops, conditional statements, and arithmetic operations in a real-world context. By working on such foundational concepts, one can grasp the intricacies of programming languages like C and cultivate logical thinking and problem-solving skills.