- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

In this problem, we are given a number N. Our task is to create a program to find the sum of the digits of the number N in bases from 2 to N/2.

So, we have to convert the base of the number to all bases from 2 to N/2 i.e. for n = 9, bases will be 2, 3, 4. And the find the sum of all digits in these bases.

**Let’s take an example to understand the problem,**

**Input **

N = 5

**Output **

2

**Explanation **

base from 2 to N/2 is 2. 52 = 101, sum of digits is 2.

To, solve this problem, we take every number from 2 to N/2 as a base. And then to calculate the sum of digits, we will repeatedly divide the N by base i.e. N = N/base, and add the remainder value to the sum. And then add the sum values found for each base to get the result.

## Example

**Program to illustrate the working of our solution **−

#include <iostream> using namespace std; int findBaseSum(int n, int base, int &sum){ while (n > 0) { sum += n % base; n /= base; } return sum; } void CalcSumOfBaseDigits(int n, int &sum){ for (int base = 2; base <= n / 2; base++) findBaseSum(n, base, sum); } int main(){ int N = 11; int sum = 0; CalcSumOfBaseDigits(N, sum); cout<<"The sum of digits of "<<N<<" written in all bases from 2 to "<<(N/2)<<" is "<<sum; return 0; }

## Output

The sum of digits of 11 written in all bases from 2 to 5 is 14

- Related Questions & Answers
- Sum of the series 2^0 + 2^1 + 2^2 +...+ 2^n in C++
- Number of digits in 2 raised to power n in C++
- Sum of series 1^2 + 3^2 + 5^2 + . . . + (2*n - 1)^2 in C++
- Sum of series 1^2 + 3^2 + 5^2 + . . . + (2*n – 1)^2
- Find sum of Series with n-th term as n^2 - (n-1)^2 in C++
- Sum of the series 1^1 + 2^2 + 3^3 + ... + n^n using recursion in C++
- Find Sum of Series 1^2 - 2^2 + 3^2 - 4^2 ... upto n terms in C++
- Count pairs (a, b) whose sum of squares is N (a^2 + b^2 = N) in C++
- Sum of the series 1.2.3 + 2.3.+ … + n(n+1)(n+2) in C
- Java Program to Find sum of Series with n-th term as n^2 – (n-1)^2
- Python Program for Find sum of Series with the n-th term as n^2 – (n-1)^2
- C++ program to find the sum of the series (1/a + 2/a^2 + 3/a^3 + … + n/a^n)
- C/C++ Program to Find the sum of Series with the n-th term as n^2 – (n-1)^2
- Minimize the sum of squares of sum of N/2 paired formed by N numbers in C++
- C/C++ Program to Find sum of Series with n-th term as n power of 2 - (n-1) power of 2

Advertisements