Given a number N in decimal base , find the sum of digits in any base B | Java Implementation to Compute Sum of Digits of Number N in Base B ; Function to compute sum of Digits of Number N in base B ; Initialize sum with 0 ; Compute unit digit of the number ; Add unit digit in sum ; Update value of Number ; Driver code
class GFG { static int sumOfDigit ( int n , int b ) { int unitDigit , sum = 0 ; while ( n > 0 ) { unitDigit = n % b ; sum += unitDigit ; n = n / b ; } return sum ; } public static void main ( String [ ] args ) { int n = 50 ; int b = 2 ; System . out . print ( sumOfDigit ( n , b ) ) ; } }