Sum of digits written in different bases from 2 to n | CPP program to find sum of digits of n in different bases from 2 to n - 1. ; function to calculate sum of digit for a given base ; Sum of digits ; Calculating the number ( n ) by taking mod with the base and adding remainder to the result and parallelly reducing the num value . ; returning the result ; function calling for multiple bases ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int solve ( int n , int base ) { int result = 0 ; while ( n > 0 ) { int remainder = n % base ; result = result + remainder ; n = n / base ; } return result ; } void printSumsOfDigits ( int n ) { for ( int base = 2 ; base < n ; ++ base ) cout << solve ( n , base ) << " ▁ " ; } int main ( ) { int n = 8 ; printSumsOfDigits ( n ) ; return 0 ; }