Given a number n, find the sum of digits of n when represented in different bases from 2 to n-1.

**Examples:**

Input :5Output :2 3 2 Representation of 5 is 101, 12, 11 in bases 2 , 3 , 4 .Input :7Output :3 3 4 3 2

- As the given question wants the sum of digits in different bases, first we have to calculate the given number of different bases and add each digit to the number of different bases.
- So, to calculate each number’s representation we will take the mod of given number by the base in which we want to represent that number.
- Then, we have to add all those mod values as the mod values obtained will represent that number in that base.
- Finally, the sum of those mod values gives the sum of digits of that number.

Below are implementations of this approach

## C++

`// CPP program to find sum of digits of ` `// n in different bases from 2 to n-1. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to calculate sum of ` `// digit for a given base ` `int` `solve(` `int` `n, ` `int` `base) ` `{ ` ` ` `// Sum of digits ` ` ` `int` `result = 0 ; ` ` ` ` ` `// Calculating the number (n) by ` ` ` `// taking mod with the base and adding ` ` ` `// remainder to the result and ` ` ` `// parallelly reducing the num value . ` ` ` `while` `(n > 0) ` ` ` `{ ` ` ` `int` `remainder = n % base ; ` ` ` `result = result + remainder ; ` ` ` `n = n / base; ` ` ` `} ` ` ` ` ` `// returning the result ` ` ` `return` `result ; ` `} ` ` ` `void` `printSumsOfDigits(` `int` `n) ` `{ ` ` ` `// function calling for multiple bases ` ` ` `for` `(` `int` `base = 2 ; base < n ; ++base) ` ` ` `cout << solve(n, base) <<` `" "` `; ` `} ` ` ` `// Driver program ` `int` `main() ` `{ ` ` ` `int` `n = 8; ` ` ` `printSumsOfDigits(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find sum of digits of ` `// n in different bases from 2 to n-1. ` `class` `GFG ` `{ ` `// function to calculate sum of ` `// digit for a given base ` `static` `int` `solve(` `int` `n, ` `int` `base) ` `{ ` ` ` `// Sum of digits ` ` ` `int` `result = ` `0` `; ` ` ` ` ` `// Calculating the number (n) by ` ` ` `// taking mod with the base and adding ` ` ` `// remainder to the result and ` ` ` `// parallelly reducing the num value . ` ` ` `while` `(n > ` `0` `) ` ` ` `{ ` ` ` `int` `remainder = n % base ; ` ` ` `result = result + remainder ; ` ` ` `n = n / base; ` ` ` `} ` ` ` ` ` `// returning the result ` ` ` `return` `result ; ` `} ` ` ` `static` `void` `printSumsOfDigits(` `int` `n) ` `{ ` ` ` `// function calling for multiple bases ` ` ` `for` `(` `int` `base = ` `2` `; base < n ; ++base) ` ` ` `System.out.print(solve(n, base)+` `" "` `); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `8` `; ` ` ` `printSumsOfDigits(n); ` `} ` `} ` `// This code is contributed by Smitha ` |

*chevron_right*

*filter_none*

## Python3

`# Python program to find sum of digits of ` `# n in different bases from 2 to n-1. ` ` ` `# def to calculate sum of ` `# digit for a given base ` `def` `solve(n, base) : ` ` ` ` ` `# Sum of digits ` ` ` `result ` `=` `0` ` ` ` ` `# Calculating the number (n) by ` ` ` `# taking mod with the base and adding ` ` ` `# remainder to the result and ` ` ` `# parallelly reducing the num value . ` ` ` `while` `(n > ` `0` `) : ` ` ` ` ` `remainder ` `=` `n ` `%` `base ` ` ` `result ` `=` `result ` `+` `remainder ` ` ` `n ` `=` `int` `(n ` `/` `base) ` ` ` ` ` `# returning the result ` ` ` `return` `result ` ` ` `def` `printSumsOfDigits(n) : ` ` ` ` ` `# def calling for ` ` ` `# multiple bases ` ` ` `for` `base ` `in` `range` `(` `2` `, n) : ` ` ` `print` `(solve(n, base), end` `=` `" "` `) ` ` ` `# Driver code ` `n ` `=` `8` `printSumsOfDigits(n) ` ` ` `# This code is contributed by Manish Shaw ` `# (manishshaw1) ` |

*chevron_right*

*filter_none*

## C#

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

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find sum of digits of ` `// n in different bases from 2 to n-1. ` ` ` `// function to calculate sum of ` `// digit for a given base ` `function` `solve(` `$n` `, ` `$base` `) ` `{ ` ` ` ` ` `// Sum of digits ` ` ` `$result` `= 0 ; ` ` ` ` ` `// Calculating the number (n) by ` ` ` `// taking mod with the base and adding ` ` ` `// remainder to the result and ` ` ` `// parallelly reducing the num value . ` ` ` `while` `(` `$n` `> 0) ` ` ` `{ ` ` ` `$remainder` `= ` `$n` `% ` `$base` `; ` ` ` `$result` `= ` `$result` `+ ` `$remainder` `; ` ` ` `$n` `= ` `$n` `/ ` `$base` `; ` ` ` `} ` ` ` ` ` `// returning the result ` ` ` `return` `$result` `; ` `} ` ` ` `function` `printSumsOfDigits(` `$n` `) ` `{ ` ` ` ` ` `// function calling for ` ` ` `// multiple bases ` ` ` `for` `(` `$base` `= 2 ; ` `$base` `< ` `$n` `; ++` `$base` `) ` ` ` `{ ` ` ` `echo` `(solve(` `$n` `, ` `$base` `)); ` ` ` `echo` `(` `" "` `); ` ` ` `} ` `} ` ` ` `// Driver code ` `$n` `= 8; ` `printSumsOfDigits(` `$n` `); ` ` ` `// This code is contributed by Ajit. ` `?> ` |

*chevron_right*

*filter_none*

Output :

1 4 2 4 3 2

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.

## Recommended Posts:

- Sum of the digits of a number N written in all bases from 2 to N/2
- Quickly convert Decimal to other bases in Python
- Minimum digits to be removed to make either all digits or alternating digits same
- Smallest number with given sum of digits and sum of square of digits
- Even digits Sum and Odd digits sum divisible by 4 and 3 respectively
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Check if a number can be written as sum of three consecutive integers
- Count different numbers that can be generated such that there digits sum is equal to 'n'
- Find smallest number with given number of digits and sum of digits
- Find the Largest number with given number of digits and sum of digits
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Check if the sum of digits of number is divisible by all of its digits
- Sum of the digits of square of the given number which has only 1's as its digits
- Find smallest number with given number of digits and sum of digits under given constraints
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Written version of Logical operators in C++
- Program to check if a given number is Lucky (all digits are different)
- Program to count digits in an integer (4 Different Methods)
- Number of digits in the nth number made of given four digits
- Minimum number of digits to be removed so that no two consecutive digits are same

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.