# Sum of the digits of a number N written in all bases from 2 to N/2

Given an integer N, the task is to find the sum of the digits of the number N written in all the bases from 2 to N / 2.

Examples:

Input: N = 6
Output: 4
In base 2, 6 is represented as 110.
In base 3, 6 is represented as 20.
Sum = 1 + 1 + 0 + 2 + 0 = 4

Input: N = 8
Output: 7

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

Approach:

• For every base from 2 to (n / 2) calculate the digits of n in the particular base with the following:
• Calculate the remainder on dividing n by base and the remainder is one of the digits of n in that base.
• Add the digit to the sum and update n as (n = n / base).
• Repeat the above steps while n > 0
• Print the sum calculated in the previous steps.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to calculate the sum of the digits of ` `// n in the given base ` `int` `solve(``int` `n, ``int` `base) ` `{ ` `    ``// Sum of digits ` `    ``int` `sum = 0; ` ` `  `    ``while` `(n > 0) { ` ` `  `        ``// Digit of n in the given base ` `        ``int` `remainder = n % base; ` ` `  `        ``// Add the digit ` `        ``sum += remainder; ` `        ``n = n / base; ` `    ``} ` ` `  `    ``return` `sum; ` `} ` ` `  `// Function to calculate the sum of ` `// digits of n in bases from 2 to n/2 ` `void` `SumsOfDigits(``int` `n) ` `{ ` `    ``// to store digit sum in all bases ` `    ``int` `sum = 0; ` ` `  `    ``// function call for multiple bases ` `    ``for` `(``int` `base = 2; base <= n / 2; ++base) ` `        ``sum += solve(n, base); ` ` `  `    ``cout << sum; ` `} ` ` `  `// Driver program ` `int` `main() ` `{ ` `    ``int` `n = 8; ` `    ``SumsOfDigits(n); ` `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach ` ` `  `import` `java.io.*; ` ` `  `class` `GFG { ` `     `  ` `  `// Function to calculate the sum of the digits of ` `// n in the given base ` `static` `int` `solve(``int` `n, ``int` `base) ` `{ ` `    ``// Sum of digits ` `    ``int` `sum = ``0``; ` ` `  `    ``while` `(n > ``0``) { ` ` `  `        ``// Digit of n in the given base ` `        ``int` `remainder = n % base; ` ` `  `        ``// Add the digit ` `        ``sum += remainder; ` `        ``n = n / base; ` `    ``} ` ` `  `    ``return` `sum; ` `} ` ` `  `// Function to calculate the sum of ` `// digits of n in bases from 2 to n/2 ` `static` `void` `SumsOfDigits(``int` `n) ` `{ ` `    ``// to store digit sum in all bases ` `    ``int` `sum = ``0``; ` ` `  `    ``// function call for multiple bases ` `    ``for` `(``int` `base = ``2``; base <= n / ``2``; ++base) ` `        ``sum += solve(n, base); ` ` `  `    ``System.out.println(sum); ` `} ` ` `  `// Driver program ` ` `  ` `  `    ``public` `static` `void` `main (String[] args) { ` `        ``int` `n = ``8``; ` `    ``SumsOfDigits(n); ` `    ``} ` `     `  `} ` `// This code is contributed by anuj_67.. `

## Python3

 `# Python 3 implementation of the approach ` `from` `math ``import` `floor ` `# Function to calculate the sum of the digits of ` `# n in the given base ` `def` `solve(n, base): ` `    ``# Sum of digits ` `    ``sum` `=` `0` ` `  `    ``while` `(n > ``0``): ` `        ``# Digit of n in the given base ` `        ``remainder ``=` `n ``%` `base ` ` `  `        ``# Add the digit ` `        ``sum` `=` `sum` `+` `remainder ` `        ``n ``=` `int``(n ``/` `base) ` `     `  `    ``return` `sum` ` `  `# Function to calculate the sum of ` `# digits of n in bases from 2 to n/2 ` `def` `SumsOfDigits(n): ` `     `  `    ``# to store digit sum in all base ` `    ``sum` `=` `0` `    ``N ``=` `floor(n``/``2``) ` `    ``# function call for multiple bases ` `    ``for` `base ``in` `range``(``2``,N``+``1``,``1``): ` `        ``sum` `=` `sum` `+` `solve(n, base) ` ` `  `    ``print``(``sum``) ` ` `  `# Driver program ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``n ``=` `8` `    ``SumsOfDigits(n) ` `     `  `# This code is contributed by  ` `# Surendra_Gangwar `

## C#

 `// C# implementation of the approach  ` `using` `System; ` ` `  `class` `GFG ` `{  ` `     `  `// Function to calculate the sum of  ` `// the digits of n in the given base ` `static` `int` `solve(``int` `n, ``int` `base1)  ` `{  ` `    ``// Sum of digits  ` `    ``int` `sum = 0;  ` ` `  `    ``while` `(n > 0) ` `    ``{  ` ` `  `        ``// Digit of n in the given base  ` `        ``int` `remainder1 = n % base1;  ` ` `  `        ``// Add the digit  ` `        ``sum += remainder1;  ` `        ``n = n / base1;  ` `    ``}  ` ` `  `    ``return` `sum;  ` `}  ` ` `  `// Function to calculate the sum of  ` `// digits of n in base1s from 2 to n/2  ` `static` `void` `SumsOfDigits(``int` `n)  ` `{  ` `    ``// to store digit sum in all bases  ` `    ``int` `sum = 0;  ` ` `  `    ``// function call for multiple bases  ` `    ``for` `(``int` `base1 = 2; ` `             ``base1 <= n / 2; ++base1)  ` `        ``sum += solve(n, base1);  ` ` `  `    ``Console.WriteLine(sum);  ` `}  ` ` `  `// Driver Code ` `public` `static` `void` `Main (String []args) ` `{  ` `    ``int` `n = 8;  ` `    ``SumsOfDigits(n);  ` `}  ` `} ` ` `  `// This code is contributed by Arnab Kundu `

## PHP

 ` 0)  ` `    ``{ ` ` `  `        ``// Digit of n in the given base ` `        ``\$remainder` `= ``\$n` `% ``\$base``; ` ` `  `        ``// Add the digit ` `        ``\$sum` `+= ``\$remainder``; ` `        ``\$n` `= ``\$n` `/ ``\$base``; ` `    ``} ` ` `  `    ``return` `\$sum``; ` `} ` ` `  `// Function to calculate the sum of ` `// digits of n in bases from 2 to n/2 ` `function` `SumsOfDigits(``\$n``) ` `{ ` `    ``// to store digit sum in all bases ` `    ``\$sum` `= 0; ` ` `  `    ``// function call for multiple bases ` `    ``for` `(``\$base` `= 2;  ` `         ``\$base` `<= ``\$n` `/ 2; ++``\$base``) ` `        ``\$sum` `+= solve(``\$n``, ``\$base``); ` ` `  `    ``echo` `\$sum``; ` `} ` ` `  `// Driver Code ` `\$n` `= 8; ` `SumsOfDigits(``\$n``); ` ` `  `// This code is contributed  ` `// by Akanksha Rai ` `?> `

Output:

```7
```

My Personal Notes arrow_drop_up pawanasipugmailcom

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.