Narcissistic Number In C++ - C++ Programming Tutorial
C++ Course / Miscellaneous / Narcissistic Number In C++

Narcissistic Number In C++

BLUF: Mastering Narcissistic Number In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Narcissistic Number In C++

C++ is renowned for its efficiency. Learn how Narcissistic Number In C++ enables low-level control and high-performance computing in the tutorial below.

In this tutorial, we will explore Narcissistic Numbers in C++. Prior to delving into Narcissistic Numbers in C++, it is essential to understand the techniques, illustrations, time complexity, and space complexity associated with them.

What is the Narcissistic Numbers?

An Armstrong Number, also known as a Narcissistic Number, is a unique number that equals the sum of its digits raised to the power of the number of digits it has. In the case of a three-digit number 'n', each digit 'd' is raised to the power of three, and the total sum of these powered digits is then compared to 'n'. If they are equal, the number is classified as narcissistic. For instance, in the number 153, the calculation 1^3 + 5^3 + 3^3 results in 153, confirming its status as a narcissistic number.

In C++, this concept is implemented by extracting individual digits through division and modulus operations, computing the power of each digit, and finally summing these results to verify their equality with the original number. The distinct nature of this property makes it a valuable exercise in programming and computational mathematics. It aids in enhancing skills related to conditional statements, loops, and various mathematical operations.

Method 1: Using Pow Function

The method includes tallying the quantity of digits within the number, followed by isolating each individual digit. The potency of every digit is established by utilizing the total digit count and executing the pow function. To ascertain whether a number is narcissistic, the total of these digit powers is computed and contrasted with the initial number.

Example:

Let's consider an example to demonstrate the concept of Narcissistic Numbers in C++.

Example

#include <iostream>
#include <cmath>
using namespace std;
int countDigits(int number)
{
    if (number == 0)
        return 0;
    return 1 + countDigits(number / 10);
}
bool Narcissistic(int number) 
{
    int digitCount = countDigits(number);
    int temp = number;
    int totalSum = 0;
    while (temp > 0)
    {
        int currentDigit = temp % 10;
        totalSum += pow(currentDigit, digitCount);
        temp /= 10;
    }
    return number == totalSum;
}
int main()
{
    int numberToCheck;
    cout << "Enter a number to check if it is a Narcissistic number: ";
    cin >> numberToCheck;
    if (Narcissistic(numberToCheck)) 
    {
        cout << numberToCheck << " is a Narcissistic number." << endl;
    } 
    else
    {
        cout << numberToCheck << " is not a Narcissistic number." << endl;
    }
    return 0;
}

Output:

Output

Enter a number to check if it is a Narcissistic number: 153
153 is a Narcissistic number.

Explanation:

This software application identifies the narcissistic nature of a given number. It starts by defining the function countDigits, which recursively determines the total count of digits in the provided integer. Utilizing this count, the function "Narcissistic" computes the sum of each digit raised to the power of the total digit count. The program then checks if this sum matches the original number to determine narcissism. In the main function, the user inputs a number which is then passed to the "Narcissistic" function for evaluation. Upon evaluation, the program informs the user if the number is narcissistic or not based on the function's return value. This methodology involves utilizing exponentiation and modulus operations. The interactive tool dynamically processes user inputs and showcases fundamental programming concepts such as conditional statements, loops, and recursive functions.

Complexity Analysis:

  • Time Complexity: O(log n): Because the number of iterations (each digit is processed once) is determined by the number of digits in the input number, the time complexity is O(log n).
  • Auxiliary Space: O(1): The auxiliary space is O(1) because only a constant amount of extra space is used regardless of the size of the input number.
  • Method 2: Simplified Method using string

Traversing through a given string, the Simplified Method iterates over each character. It computes the exponentiation of the corresponding digit (obtained from the character) raised to the length of the string. This process signifies the count of digits present in each character, eliminating the need for traditional arithmetic operations on the original number.

Example:

Let's consider another instance to demonstrate the concept of a Narcissistic Number in the C++ programming language.

Example

#include <iostream>
#include <cmath>
#include <string>
using namespace std;
string checkNarcissistic(string inputNumber) 
{
    int calculatedSum = 0;
    int digitCount = inputNumber.length();
    for (int i = 0; i < digitCount; i++)
    {
        int currentDigit = inputNumber[i] - '0';
        calculatedSum += pow(currentDigit, digitCount);
    }
    int originalNumber = stoi(inputNumber);
    if (originalNumber == calculatedSum)
    {
        return "Yes, the number is Narcissistic.";
    } 
    else
    {
        return "No, the number is not Narcissistic.";
    }
}
int main() 
{
    string numberInput;
    cout << "Enter a number to check if it is a Narcissistic number: ";
    cin >> numberInput;
    string result = checkNarcissistic(numberInput);
    cout << result << endl;
    return 0;
}

Output:

Output

Enter a number to check if it is a Narcissistic number: 154
No, the number is not Narcissistic.

Explanation:

This C++ program is designed to verify if a provided number is a narcissistic number. It defines a function named checkNarcissistic that takes a string input representing the number, calculates the sum of each digit raised to the power of the total number of digits, and then compares this sum to the original number. The function confirms the narcissistic property of the number by returning a message when the calculated sum matches the original number. Within the main function, the user is prompted to input a number, which is subsequently processed by the checkNarcissistic function. The resulting message indicating whether the number is narcissistic or not is then displayed. The program handles the number manipulation tasks by utilizing string operations and mathematical functions like type conversion and exponentiation.

Complexity Analysis:

  • Time Complexity: O(n log n): The string length (n) and the logarithmic operations required to raise the digits to a power (log n) during the computation influence the time complexity.
  • Auxiliary Space: O(1): The space complexity is constant because only a few variables are used, regardless of the input size.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience