Given a number N in decimal base, find the sum of digits in any base B - GeeksforGeeks

06 Nov, 2021
Given a number N in decimal base, the task is to find the sum of digits of the number in any base B. Examples:  
Input: N = 100, B = 8 Output: 9 Explanation: (100)8 = 144 Sum(144) = 1 + 4 + 4 = 9Input: N = 50, B = 2 Output: 3 Explanation: (50)2 = 110010 Sum(110010) = 1 + 1 + 0 + 0 + 1 + 0 = 3  
 
Approach: Find unit digit by performing modulo operation on number N by base B and updating the number again by N = N / B and update sum by adding the unit digit at each step. Below is the implementation of above approach  
C++
Java
Python3
C#
Javascript
// C++ Implementation to Compute Sum of// Digits of Number N in Base B #include <iostream>using namespace std; // Function to compute sum of// Digits of Number N in base Bint sumOfDigit(int n, int b){         // Initialize sum with 0    int unitDigit, sum = 0;    while (n > 0) {                 // Compute unit digit of the number        unitDigit = n % b;         // Add unit digit in sum        sum += unitDigit;         // Update value of Number        n = n / b;    }    return sum;} // Driver functionint main(){    int n = 50;    int b = 2;    cout << sumOfDigit(n, b);    return 0;}
// Java Implementation to Compute Sum of// Digits of Number N in Base Bclass GFG{ // Function to compute sum of// Digits of Number N in base Bstatic int sumOfDigit(int n, int b){         // Initialize sum with 0    int unitDigit, sum = 0;    while (n > 0)    {                 // Compute unit digit of the number        unitDigit = n % b;         // Add unit digit in sum        sum += unitDigit;         // Update value of Number        n = n / b;    }    return sum;} // Driver codepublic static void main(String[] args){    int n = 50;    int b = 2;    System.out.print(sumOfDigit(n, b));}} // This code is contributed by PrinciRaj1992
# Python3 Implementation to Compute Sum of# Digits of Number N in Base B # Function to compute sum of# Digits of Number N in base Bdef sumOfDigit(n, b):     # Initialize sum with 0    unitDigit = 0    sum = 0    while (n > 0):                 # Compute unit digit of the number        unitDigit = n % b         # Add unit digit in sum        sum += unitDigit         # Update value of Number        n = n // b     return sum # Driver coden = 50b = 2print(sumOfDigit(n, b)) # This code is contributed by ApurvaRaj
// C# Implementation to Compute Sum of// Digits of Number N in Base Busing System; class GFG{ // Function to compute sum of// Digits of Number N in base Bstatic int sumOfDigit(int n, int b){         // Initialize sum with 0    int unitDigit, sum = 0;    while (n > 0)    {                 // Compute unit digit of the number        unitDigit = n % b;         // Add unit digit in sum        sum += unitDigit;         // Update value of Number        n = n / b;    }    return sum;} // Driver codepublic static void Main(String[] args){    int n = 50;    int b = 2;    Console.Write(sumOfDigit(n, b));}} // This code is contributed by PrinciRaj1992
<script> // Javascript Implementation to Compute Sum of// Digits of Number N in Base B // Function to compute sum of// Digits of Number N in base Bfunction sumOfDigit(n, b){         // Initialize sum with 0    var unitDigit, sum = 0;    while (n > 0)    {                 // Compute unit digit of the number        unitDigit = n % b;         // Add unit digit in sum        sum += unitDigit;         // Update value of Number        n = parseInt(n / b);    }    return sum;} // Driver functionvar n = 50;var b = 2;document.write(sumOfDigit(n, b)); // This code is contributed by rutvik_56.</script>
3
 
Time Complexity: O(N), N = number of digits
Auxiliary Space: O(1)
ApurvaRaj
princiraj1992
rutvik_56
sweetyty
arorakashish0911
rohitsingh07052
base-conversion
number-digits
Technical Scripter 2019
Mathematical
Technical Scripter
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Find all factors of a natural number | Set 1
Check if a number is Palindrome
Program to print prime numbers from 1 to N.
Program to add two binary strings
Program to multiply two matrices
Fizz Buzz Implementation
Find pair with maximum GCD in an array
Find Union and Intersection of two unsorted arrays
Count all possible paths from top left to bottom right of a mXn matrix
Count ways to reach the n'th stair