Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C++ Exercises: Find Narcissistic decimal numbers within a specific range

C++ Numbers: Exercise-37 with Solution

Write a program in C++ to find Narcissistic decimal numbers within a specific range.

Sample Solution:

C++ Code :

#include <iostream>
#include <cmath>
using namespace std;
int main() 
{
    int nl,nu;
 cout << "\n\n Find the Narcissistic decimal numbers between a specific range: \n";
 cout << " --------------------------------------------------------------------\n";
 	cout << " Input the lower limit: ";
    cin>>nl;	
	cout << " Input a upper limit: ";
    cin>>nu;		
  cout << " The Narcissistic decimal numbers between "<<nl<<" and "<<nu<<" are: \n";
    int i,ctr,j,orn,n,m,sum;
    for(orn=nl;orn<=nu;orn++)
    {
    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<<" ";
    }
}
	cout<<endl;
}

Sample Output:

Find the Narcissistic decimal numbers between a specific range:       
 --------------------------------------------------------------------  
 Input the lower limit: 25                                             
 Input a upper limit: 200                                              
 The Narcissistic decimal numbers between 25 and 200 are:              
 153 

Flowchart:

Flowchart: Find Narcissistic decimal numbers within a specific range

C++ Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C++ to generate Mersenne primes within a range of numbers.
Next: Write a program in C++ to check whether a given number is palindrome or not.

What is the difficulty level of this exercise?