Basic Programming C++ Examples

C++ Program to Generate and show first 15 Narcissistic decimal numbers

The Program in C++ Program to Generate and show first 15 Narcissistic decimal numbers is given below:

#include <iostream>
#include <cmath>

using namespace std;

bool isNarcissistic(int n) {
    int len = log10(n) + 1;
    int sum = 0, temp = n;

    while (temp) {
        int digit = temp % 10;
        sum += pow(digit, len);
        temp /= 10;
    }

    return (sum == n);
}

int main() {
    int count = 0;
    int num = 1;

    cout << "Hello Codeauri Family,the first 15 Narcissistic decimal numbers are:" << endl;
    while (count < 15) {
        if (isNarcissistic(num)) {
            cout << num << " ";
            count++;
        }
        num++;
    }
    cout << endl;

    return 0;
}

Output:

Hello Codeauri Family,the first 15 Narcissistic decimal numbers are:
1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208

Pro-Tips💡

This code defines a function called “isNarcissistic” that takes an integer as input, and returns true if the number is narcissistic and false otherwise. The function performs the following operations:

  • Calculates the number of digits in the input integer.
  • Extracts each digit from the input integer, raises it to the power of the number of digits, and adds the result to a sum.
  • Returns true if the sum is equal to the input integer, and false otherwise.

The main function then prints the first 15 narcissistic decimal numbers by incrementing an integer (num) and

calling the “isNarcissistic” function on each iteration, counting and printing the numbers that are narcissistic until 15 are found.

Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

Programming Languages( Types, Pros and Cons)-Codeauri

What are the types or levels of Programming Languages? The types or levels of programming languages are divided into two types: Types Of Programming Languages: 1.Machine-level Language :1st…

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X