Sum of digits written in different bases from 2 to n | Java program to find the sum of digits of n in different base1s from 2 to n - 1. ; function to calculate sum of digit for a given base1 ; Sum of digits ; Calculating the number ( n ) by taking mod with the base1 and adding remainder to the result and parallelly reducing the num value . ; returning the result ; function calling for multiple base1s ; Driver Code
using System ; class GFG { static int solve ( int n , int base1 ) { int result = 0 ; while ( n > 0 ) { int remainder = n % base1 ; result = result + remainder ; n = n / base1 ; } return result ; } static void printSumsOfDigits ( int n ) { for ( int base1 = 2 ; base1 < n ; ++ base1 ) Console . Write ( solve ( n , base1 ) + " ▁ " ) ; } public static void Main ( ) { int n = 8 ; printSumsOfDigits ( n ) ; } }