Check Narcissistic Number
Check Narcissistic Number in C++ (4 Programs)
C++ Check Narcissistic Number Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#include <iostream>
#include <cmath>
using namespace std;
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;
}
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;
}Enter a number: 153 153 is a Narcissistic number
Understanding Check Narcissistic Number
This program teaches you how to check if a number is a Narcissistic number (also called Armstrong number) in C++. A Narcissistic number is a special number that equals the sum of its digits, each raised to the power of the number of digits. This is a fascinating number theory problem that helps students understand digit manipulation, exponentiation, and mathematical patterns.
---
1. What This Program Does
The program checks whether a given number is a Narcissistic number. For example:
Example:
---
2. Header Files Used
---
3. 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
:
---
4. Function: isNarcissistic()
bool isNarcissistic(int num) {
int original = num;
int n = 0, sum = 0;
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:
---
// Count digits5. 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
---
6. Understanding the Algorithm Components
Counting Digits
:
Extracting Digits
:
Raising to Power
:
Accumulation
:
---
7. Main Function
int num;
cin >> num;
if (isNarcissistic(num)) {
cout << num << " is a Narcissistic number" << endl;
} else {
cout << num << " is not a Narcissistic number" << endl;
}
return 0;
}
int main() {Process Flow
:
---
8. 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;
int digit = c - '0';
sum += pow(digit, n);
}
return sum == num;Method 3: Using Recursion
int countDigits(int num) {
if (num == 0) return 0;
}
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;
}
return 1 + countDigits(num / 10);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;9. Displaying the Result
The program prints:
Output (for input 153):
153 is a Narcissistic number
Or if not a Narcissistic number:
123 is not a Narcissistic number
---
cout << num << " is a Narcissistic number" << endl;10. Why Are They Called "Narcissistic"?
Origin of Name
:
Mathematical Beauty
:
---
11. Common Narcissistic Numbers
1-digit (all are Narcissistic)
:
3-digit
:
4-digit
:
---
12. Common Use Cases
Number Theory
:
Algorithm Practice
:
Interview Questions
:
---
13. Important Considerations
Edge Cases
:
Large Numbers
:
Efficiency
:
---
14. return 0;
This ends the program successfully.
---
Summary
This program is fundamental for beginners learning digit manipulation, understanding special number properties, exponentiation, and preparing for more complex number theory problems in C++ programs.
Let us now understand every line and the components of the above program.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.
Practical Learning Notes for Check Narcissistic Number
This C++ program is part of the "Advanced Number Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.