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

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
Explnantion:
(100)8 = 144
Sum(144) = 1 + 4 + 4 = 9

Input: N = 50, B = 2
Output: 3
Explnantion:
(50)2 = 110010
Sum(110010) = 1 + 1 + 0 + 0 + 1 + 0 = 3

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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++

 `// C++ Implementation to Compute Sum of ` `// Digits of Number N in Base B ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to compute sum of ` `// Digits of Number N in base B ` `int` `sumOfDigit(``int` `n, ``int` `b) ` `{ ` `     `  `    ``// Initilize 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 function ` `int` `main() ` `{ ` `    ``int` `n = 50; ` `    ``int` `b = 2; ` `    ``cout << sumOfDigit(n, b); ` `    ``return` `0; ` `} `

## Java

 `// Java Implementation to Compute Sum of ` `// Digits of Number N in Base B ` `class` `GFG ` `{ ` ` `  `// Function to compute sum of ` `// Digits of Number N in base B ` `static` `int` `sumOfDigit(``int` `n, ``int` `b) ` `{ ` `     `  `    ``// Initilize 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 code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `n = ``50``; ` `    ``int` `b = ``2``; ` `    ``System.out.print(sumOfDigit(n, b)); ` `} ` `} ` ` `  `// This code is contributed by PrinciRaj1992 `

## Python3

 `# Python3 Implementation to Compute Sum of ` `# Digits of Number N in Base B ` ` `  `# Function to compute sum of ` `# Digits of Number N in base B ` `def` `sumOfDigit(n, b): ` ` `  `    ``# Initilize 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 code ` `n ``=` `50` `b ``=` `2` `print``(sumOfDigit(n, b)) ` ` `  `# This code is contributed by ApurvaRaj `

## C#

 `// C# Implementation to Compute Sum of  ` `// Digits of Number N in Base B  ` `using` `System; ` ` `  `class` `GFG  ` `{  ` ` `  `// Function to compute sum of  ` `// Digits of Number N in base B  ` `static` `int` `sumOfDigit(``int` `n, ``int` `b)  ` `{  ` `     `  `    ``// Initilize 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 code  ` `public` `static` `void` `Main(String[] args)  ` `{  ` `    ``int` `n = 50;  ` `    ``int` `b = 2;  ` `    ``Console.Write(sumOfDigit(n, b));  ` `}  ` `} ` ` `  `// This code is contributed by PrinciRaj1992 `

Output:

```3
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : ApurvaRaj, princiraj1992

Article Tags :
Practice Tags :

5

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.