Harshad ( Or Niven ) Number | C ++ implementation of above approach ; Converting integer to string ; Initialising sum to 0 ; Traversing through the string ; Converting character to int ; Comparing number and sum ; Driver Code ; Passing this number to get result function
#include <bits/stdc++.h> NEW_LINE using namespace std ; string checkHarshad ( int n ) { string st = to_string ( n ) ; int sum = 0 ; int length = st . length ( ) ; for ( char i : st ) { sum = sum + ( i - '0' ) ; } if ( n % sum == 0 ) { return " Yes " ; } else { return " No " ; } } int main ( ) { int number = 18 ; cout << checkHarshad ( number ) << endl ; }