Narcissistic number | CPP program for checking of Narcissistic number ; Traversing through the string ; Since ascii value of numbers starts from 48 so we subtract it from sum ; Converting string to integer ; Comparing number and sum ; Driver Code
#include <bits/stdc++.h> NEW_LINE #include <string.h> NEW_LINE using namespace std ; string getResult ( string st ) { int sum = 0 ; int length = st . length ( ) ; for ( int i = 0 ; i < length ; i ++ ) { sum = sum + pow ( st [ i ] - '0' , length ) ; } int number = stoi ( st ) ; if ( number == sum ) return " yes " ; else return " no " ; } int main ( ) { string st = "153" ; cout << getResult ( st ) ; return 0 ; }