C++ Exercises: Generate and show the first 15 Narcissistic decimal numbers
C++ Numbers: Exercise-13 with Solution
Write a program in C++ to generate and show the first 15 Narcissistic decimal numbers.
Pictorial Presentation:
Sample Solution:
C++ Code :
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
cout << "\n\n Find the first 15 Narcissistic decimal numbers: \n";
cout << " -----------------------------------------------------\n";
cout << " The first 15 Narcissistic decimal numbers are: \n";
int i,ctr,j,orn,n,m,sum;
orn=1;
for (i=1;i<=15;)
{
ctr=0;
sum=0;
n=orn;
while(n>0)
{
n=n/10;
ctr++;
}
n=orn;
while(n>0)
{
m=n % 10;
sum=sum+pow(m,ctr);
n=n/10;
}
if(sum==orn)
{
cout<<orn<<" ";
i++;
}
orn++;
}
cout<<endl;
}
Sample Output:
Find the first 15 narcissistic decimal numbers: ----------------------------------------------------- The first 15 narcissistic decimal numbers are: 1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208
Flowchart:
C++ Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a program in C++ to find the Lychrel numbers and the number of Lychrel number within the range 1 to 1000 (after 500 iteration).
Next: Write a program in C++ to display the first 10 Lucus numbers.
What is the difficulty level of this exercise?