# Find the total count of numbers up to N digits in a given base B

Given two integers **N** and **B**, the task is to find the count of natural numbers of Base B up to **N** digits.

**Examples:**

Input:N = 2, B = 10

Output:99

Explanation:

1, 2, 3, 4, 5, 6, 7, 8, 9 are 1 digit Natural numbers of Base 10.

10, 11, 12………99 are 2 digit Natural numbers of Base 10

So, total = 9 + 90 = 99

Input:N = 2, B = 16

Output:255

Explanation:

There are a total of 240 two digit hexadecimal numbers and 15 one digit hexadecimal numbers.

Therefore, 240 + 15 = 255.

**Approach:** On observing carefully the count of numbers with N digits in base B is a geometric progression formed with the first term being **(B – 1)** and a common ratio of **B**.

Therefore,

Nth term = Number of natutal numbers of N digits in Base B = (B – 1) * B

^{N – 1}

Finally, count of all natural numbers in Base B up to N digits can be found out by iterating a loop from 1 to N and calculating the sum of i^{th} term using the above formula.

Below is the implementation of the above approach:

## C++

`// C++ implementation to find the count ` `// of natural numbers upto N digits ` ` ` `#include <bits/stdc++.h> ` ` ` `using` `namespace` `std; ` ` ` `// Function to return the count of ` `// natural numbers upto N digits ` `int` `count(` `int` `N, ` `int` `B) ` `{ ` ` ` `int` `sum = 0; ` ` ` ` ` `// Loop to iterate from 1 to N ` ` ` `// and calculating number of ` ` ` `// natural numbers for every 'i'th digit. ` ` ` `for` `(` `int` `i = 1; i <= N; i++) { ` ` ` `sum += (B - 1) * ` `pow` `(B, i - 1); ` ` ` `} ` ` ` `return` `sum; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `N = 2, B = 10; ` ` ` `cout << count(N, B); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to find the count ` `// of natural numbers upto N digits ` ` ` `class` `GFG{ ` ` ` `// Function to return the count of ` `// natural numbers upto N digits ` `static` `int` `count(` `int` `N, ` `int` `B) ` `{ ` ` ` `int` `sum = ` `0` `; ` ` ` ` ` `// Loop to iterate from 1 to N ` ` ` `// and calculating number of ` ` ` `// natural numbers for every 'i'th digit. ` ` ` `for` `(` `int` `i = ` `1` `; i <= N; i++){ ` ` ` `sum += (B - ` `1` `) * Math.pow(B, i - ` `1` `); ` ` ` `} ` ` ` `return` `sum; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `2` `, B = ` `10` `; ` ` ` `System.out.print(count(N, B)); ` `} ` `} ` ` ` `// This code is contributed by gauravrajput1 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to find the count ` `# of natural numbers up to N digits ` ` ` `from` `math ` `import` `pow` ` ` `# Function to return the count of ` `# natural numbers upto N digits ` `def` `count(N, B): ` ` ` `sum` `=` `0` ` ` ` ` `# Loop to iterate from 1 to N ` ` ` `# and calculating number of ` ` ` `# natural numbers for every 'i'th digit. ` ` ` `for` `i ` `in` `range` `(` `1` `, N` `+` `1` `): ` ` ` `sum` `+` `=` `(B ` `-` `1` `) ` `*` `pow` `(B, i ` `-` `1` `) ` ` ` `return` `sum` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `N ` `=` `2` ` ` `B ` `=` `10` ` ` `print` `(` `int` `(count(N, B))) ` ` ` `# This code is contributed by Bhupendra_Singh ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to find the count ` `// of natural numbers upto N digits ` `using` `System; ` `using` `System.Collections.Generic; ` `class` `GFG{ ` ` ` `// Function to return the count of ` `// natural numbers upto N digits ` `static` `int` `count(` `int` `N, ` `int` `B) ` `{ ` ` ` `int` `sum = 0; ` ` ` ` ` `// Loop to iterate from 1 to N ` ` ` `// and calculating number of ` ` ` `// natural numbers for every ` ` ` `// 'i'th digit. ` ` ` `for` `(` `int` `i = 1; i <= N; i++) ` ` ` `{ ` ` ` `sum += (` `int` `)((B - 1) * Math.Pow(B, i - 1)); ` ` ` `} ` ` ` `return` `sum; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `N = 2, B = 10; ` ` ` ` ` `Console.Write(count(N, B)); ` `} ` `} ` ` ` `// This code is contributed by amal kumar choubey ` |

*chevron_right*

*filter_none*

**Output:**

99

**Time Complexity:** O(N)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Count total number of N digit numbers such that the difference between sum of even and odd digits is 1
- Given a number N in decimal base, find the sum of digits in any base B
- Given a number N in decimal base, find number of its digits in any base (base b)
- C++ program to find all numbers less than n, which are palindromic in base 10 and base 2.
- All possible numbers of N digits and base B without leading zeros
- Find the count of numbers that can be formed using digits 3, 4 only and having length at max N.
- Count total number of digits from 1 to n
- Total numbers with no repeated digits in a range
- Total number of non-decreasing numbers with n digits
- Find the total Number of Digits in (N!)N
- Count of N-digit numbers in base K with no two consecutive zeroes
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Count total set bits in all numbers from 1 to n | Set 2
- Count total set bits in all numbers from 1 to n
- Count total set bits in all numbers from 1 to N | Set 3
- Count of total subarrays whose sum is a Fibonacci Numbers
- Count total unset bits in all the numbers from 1 to N
- Python map function | Count total set bits in all numbers from 1 to n

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.