Answer to Question #198403 in C++ for M Kamran

Question #198403

Write a function to find Narcissistic number



1
Expert's answer
2021-05-25T07:35:09-0400
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
void findNarcissistic(int max){
    int pwr = 0, sum = 0;
    string s;
    for(int i = 0; i <= max; i++){
        sum = 0;
        s = to_string(i);
        pwr = s.length();
        for(int j = 0; j < pwr; j++){
            sum += (int)pow(s[j] - '0', pwr);
        }
        if(i == sum) cout<<i<<endl;
    }
}
int main(){
    findNarcissistic(100000);
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog