C Exercise Example13
## C Exercise Example 13: Narcissistic Numbers (Armstrong Numbers)
In number theory, a **Narcissistic number** (also known as an **Armstrong number**, a **pluperfect digital invariant**, or a **plus perfect number**) is a number that is the sum of its own digits each raised to the power of the number of digits.
This tutorial demonstrates how to find and print these numbers using the C programming language.
---
### Problem Description
Write a C program to find and print all 3-digit "Narcissistic numbers".
A 3-digit Narcissistic number is an integer such that the sum of the cubes of its digits is equal to the number itself.
**Example:**
$153$ is a Narcissistic number because:
$$1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153$$
---
### Method 1: Finding 3-Digit Narcissistic Numbers
#### Algorithm Analysis
To find all 3-digit Narcissistic numbers, we can iterate through all possible 3-digit integers (from $100$ to $999$) using a `for` loop. For each number, we extract its individual digits (hundreds, tens, and units), calculate the sum of their cubes, and check if the sum equals the original number.
* **Units digit (ones):** `i % 10`
* **Tens digit:** `(i / 10) % 10`
* **Hundreds digit:** `(i / 100) % 10`
#### Source Code
```c
#include
int main() {
int i, x, y, z;
printf("3-digit Narcissistic numbers are:\n");
// Iterate through all 3-digit numbers from 100 to 999
for (i = 100; i < 1000; i++) {
// Extract individual digits
x = i % 10; // Units place
y = (i / 10) % 10; // Tens place
z = (i / 100) % 10; // Hundreds place
// Calculate the sum of the cubes of the digits
int sum = x * x * x + y * y * y + z * z * z;
// If the sum equals the original number, it is a Narcissistic number
if (i == sum) {
printf("%d\n", i);
}
}
return 0;
}
```
#### Output
```text
3-digit Narcissistic numbers are:
153
370
371
407
```
---
### Method 2: Finding Narcissistic Numbers in a Custom Range (Dynamic Digits)
The definition of a Narcissistic number can be extended to $n$ digits, where the sum of the $n$-th powers of its digits equals the number itself.
The following program allows the user to input a custom range and dynamically calculates Narcissistic (Armstrong) numbers of any digit length within that range.
#### Source Code
```c
#include
#include
// Function declarations
int countDigits(int num);
int isArmstrong(int num);
int main() {
int start, end;
// Input range from user
printf("Enter the start of the range: ");
if (scanf("%d", &start) != 1) return 1;
printf("Enter the end of the range: ");
if (scanf("%d", &end) != 1) return 1;
// Output the Narcissistic numbers within the specified range
printf("Armstrong numbers between %d and %d are:\n", start, end);
for (int i = start; i <= end; i++) {
if (isArmstrong(i)) {
printf("%d\n", i);
}
}
return 0;
}
// Helper function to calculate the number of digits in an integer
int countDigits(int num) {
int count = 0;
while (num != 0) {
num /= 10;
count++;
}
return count;
}
// Helper function to check if a number is an Armstrong/Narcissistic number
int isArmstrong(int num) {
int originalNum = num;
int digits = countDigits(num);
int sum = 0;
while (num != 0) {
int digit = num % 10;
// Add the digit raised to the power of total digits to the sum
sum += round(pow(digit, digits));
num /= 10;
}
return sum == originalNum;
}
```
#### Example Output
```text
Enter the start of the range: 100
Enter the end of the range: 10000
Armstrong numbers between 100 and 10000 are:
153
370
371
407
1634
8208
9474
```
---
### Key Considerations
1. **Integer Division:** In C, dividing an integer by another integer (e.g., `i / 10`) discards the fractional part. This behavior is highly useful for stripping the last digit off a number.
2. **Floating-Point Precision:** When using the `pow()` function from ``, the return type is `double`. Due to potential floating-point precision inaccuracies, it is recommended to wrap `pow()` in the `round()` function (from ``) when assigning or adding it to an integer variable.
3. **Compilation Flag:** If you are compiling Method 2 using `gcc` on Linux/Unix systems, remember to link the math library using the `-lm` flag:
```bash
gcc program.c -o program -lm
```
YouTip