Check Narcissistic Number in C++
Check for a narcissistic number in C++: count the digits, then add each digit raised to that count. Traces 153 and shows where pow rounding can bite.
1#include <iostream>2#include <cmath>3using namespace std;45bool isNarcissistic(int num) {6 int original = num;7 int n = 0, sum = 0;89 // Count digits10 int temp = num;11 while (temp != 0) {12 temp /= 10;13 n++;14 }1516 // Calculate sum of digits raised to power n17 temp = num;18 while (temp != 0) {19 int digit = temp % 10;20 sum += pow(digit, n);21 temp /= 10;22 }2324 return sum == original;25}2627int main() {28 int num;29 cout << "Enter a number: ";30 cin >> num;3132 if (isNarcissistic(num)) {33 cout << num << " is a Narcissistic number" << endl;34 } else {35 cout << num << " is not a Narcissistic number" << endl;36 }3738 return 0;39}
Sample Output
153 is a Narcissistic number
1 + 125 + 27.
In this one
- How to write the Armstrong test as a reusable function
- Why counting digits comes first
- How pow converts back to int when added to sum
- Known narcissistic numbers to test with
How it works
- 1
Read 153.
- 2
Count digits: 3.
- 3
Add 3³ + 5³ + 1³ = 153.
- 4
Compare with the original and return true.
Edge cases
pow rounding
Some libraries return 124.999999 for pow(5, 3).
sum += pow(...) converts the double to int by truncation, so sum could be one short. Using a small integer power loop removes the risk.
Input 0
Enter 0.
Both loops are skipped, sum is 0, and 0 is reported as narcissistic.
What's going on
The program checks whether a given number is a Narcissistic number. For example:
A Narcissistic number must satisfy: sum of (each digit^number_of_digits) = number
Example:
Header Files Used
Understanding Narcissistic Numbers
Definition:
A Narcissistic number (Armstrong number) is a number that equals the sum of its digits, each raised to the power of the number of digits.
Mathematical Expression:
If n has d digits and digits are d₁, d₂, ..., dₐ, then:
n = d₁^d + d₂^d + ... + dₐ^d
Examples:
Known Narcissistic Numbers:
Function: isNarcissistic()
bool isNarcissistic(int num) {
int original = num;
int n = 0, sum = 0;
// Count digits
int temp = num;
while (temp != 0) {
temp /= 10;
n++;
}
// Calculate sum of digits raised to power n
temp = num;
while (temp != 0) {
int digit = temp % 10;
sum += pow(digit, n);
temp /= 10;
}
return sum == original;
}
This function:
Step-by-Step Algorithm
Let's trace through the algorithm for num = 153:
Step 1: Count Digits
Iteration 1:
Iteration 2:
Iteration 3:
Result: n = 3 (153 has 3 digits)
Step 2: Calculate Sum of Digit Powers
Iteration 1:
Iteration 2:
Iteration 3:
Step 3: Compare
Result: 153 is a Narcissistic number.
Understanding the Algorithm Components
Counting Digits:
Extracting Digits:
Raising to Power:
Accumulation:
Main Function
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if (isNarcissistic(num)) {
cout << num << " is a Narcissistic number" << endl;
} else {
cout << num << " is not a Narcissistic number" << endl;
}
return 0;
}
Process Flow:
Other Methods (Mentioned but not shown in code)
Method 2: Using String Conversion
string numStr = to_string(num);
int n = numStr.length();
int sum = 0;
for (char c : numStr) {
int digit = c - '0';
sum += pow(digit, n);
}
return sum == num;
Method 3: Using Recursion
int countDigits(int num) {
if (num == 0) return 0;
return 1 + countDigits(num / 10);
}
int sumDigitPowers(int num, int power) {
if (num == 0) return 0;
return pow(num % 10, power) + sumDigitPowers(num / 10, power);
}
bool isNarcissistic(int num) {
int n = countDigits(num);
return sumDigitPowers(num, n) == num;
}
Method 4: Using Array
int digits[10], count = 0;
int temp = num;
while (temp != 0) {
digits[count++] = temp % 10;
temp /= 10;
}
int sum = 0;
for (int i = 0; i < count; i++) {
sum += pow(digits[i], count);
}
return sum == num;
Displaying the Result
The program prints:
cout << num << " is a Narcissistic number" << endl;
Output (for input 153):
153 is a Narcissistic number
Or if not a Narcissistic number:
123 is not a Narcissistic number
Why Are They Called "Narcissistic"?
Origin of Name:
Mathematical Beauty:
Common Narcissistic Numbers
1-digit (all are Narcissistic):
3-digit:
4-digit:
Common Use Cases
Number Theory:
Algorithm Practice:
Interview Questions:
Important Considerations
Edge Cases:
Large Numbers:
Efficiency:
return 0;
This ends the program successfully.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Is 9474 narcissistic?
Pick an answer to check it.
Question 2
Why count digits first?
Pick an answer to check it.
Questions people ask
Narcissistic vs Armstrong?
Same definition, two names.
Four-digit examples?
1634, 8208, 9474.
How is this different from the loop-topic Armstrong page?
The logic lives in a bool function here, so main only reads and prints.