Given a positive integer **N**, the task is to find the total number of digits in the concatenation of the first N positive integers.

**Examples:**

Input:N = 10

Output:11

Explanation:

The number formed is 12345678910.

Hence, the total number of digits = 11

Input:N = 20

Output:31

Explanation:

The number formed is 1234567891011121314151617181920

Hence, the total number of digits = 31

**Approach:** Lets make an observation with the examples.

- Let N = 13. So, the digits present in all the numbers between 1 to 13 at one’s place are 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3.
- Similarly, the digits at the tens place are 1, 1, 1, 1.
- So, total ones place digits from 1 to 13 are 13(13 – 0).
- Similarly, the total tens place digits are 4(13 – 9).
- Now, lets look at another number to understand the pattern. Let N = 234. So, the digits at units place are 1(24 times), 2(24 times), 3(24 times), 4(24 times), 5(23 times), 6(23 times), 7(23 times), 8(23 times), 9(23 times), 0(23 times). Hence, 23 * 6 + 24 * 4 = 234.
- Similarly, the digits at tens place are 234 – 9 = 225 because from 1 to 234, only 1 – 9 are the single-digit numbers.
- Lastly, the number of digits at the hundredths place is 234 – 99 = 225 as from 1 to 234, only 1 – 9 are the single-digit numbers and 1 – 99 are the double-digit numbers.
- Therefore, the total number of digits formed when concatenated is 234(234 – 1 + 1) + 225(234 – 10 + 1) + 135(234 – 100 + 1) = 594.
- Hence, the idea is to subtract 0, 9, 99, 999 …. from N to get the number of digits at every place and the summation of all of this is the required answer.

On generalizing the above pattern, the following formula is generated:

Below is the implementation of the above approach:

## C++

`// C++ program to find the number of ` `// digits after concatenating the ` `// first N positive integers ` ` ` `#include <iostream> ` `#include <math.h> ` `using` `namespace` `std; ` ` ` `// Function to find the number of ` `// digits after concatenating the ` `// first N positive integers ` `void` `numberOfDigits(` `int` `N) ` `{ ` ` ` `int` `nod = ` `floor` `(` `log10` `(N) + 1); ` ` ` `int` `toDecrease ` ` ` `= (` `pow` `(10, nod) - 1) / 9; ` ` ` `cout << (N + 1) * nod - toDecrease ` ` ` `<< endl; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `N = 13; ` ` ` `numberOfDigits(N); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the number of ` `// digits after concatenating the ` `// first N positive integers ` `class` `GFG{ ` ` ` `// Function to find the number of ` `// digits after concatenating the ` `// first N positive integers ` `static` `void` `numberOfDigits(` `int` `N) ` `{ ` ` ` `int` `nod = (` `int` `)Math.floor(Math.log10(N) + ` `1` `); ` ` ` `int` `toDecrease = (` `int` `)(Math.pow(` `10` `, nod) - ` `1` `) / ` `9` `; ` ` ` ` ` `System.out.print((N + ` `1` `) * nod - toDecrease); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `13` `; ` ` ` `numberOfDigits(N); ` `} ` `} ` ` ` `// This code is contributed by shivanisinghss2110 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find the number ` `# of digits after concatenating the ` `# first N positive integers ` `from` `math ` `import` `log10, floor ` ` ` `# Function to find the number of ` `# digits after concatenating the ` `# first N positive integers ` `def` `numberOfDigits(N): ` ` ` ` ` `nod ` `=` `floor(log10(N) ` `+` `1` `); ` ` ` `toDecrease ` `=` `(` `pow` `(` `10` `, nod) ` `-` `1` `) ` `/` `/` `9` ` ` ` ` `print` `((N ` `+` `1` `) ` `*` `nod ` `-` `toDecrease) ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `N ` `=` `13` ` ` ` ` `numberOfDigits(N) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the number of ` `// digits after concatenating the ` `// first N positive integers ` `using` `System; ` `class` `GFG{ ` ` ` `// Function to find the number of ` `// digits after concatenating the ` `// first N positive integers ` `static` `void` `numberOfDigits(` `int` `N) ` `{ ` ` ` `int` `nod = (` `int` `)Math.Floor(Math.Log10(N) + 1); ` ` ` `int` `toDecrease = (` `int` `)(Math.Pow(10, nod) - 1) / 9; ` ` ` ` ` `Console.Write((N + 1) * nod - toDecrease); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `N = 13; ` ` ` `numberOfDigits(N); ` `} ` `} ` ` ` `// This code is contributed by Nidhi_Biet ` |

*chevron_right*

*filter_none*

**Output:**

17

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:

- Count positive integers with 0 as a digit and maximum 'd' digits
- Find the number of positive integers less than or equal to N that have an odd number of digits
- Count of integers of length N and value less than K such that they contain digits only from the given set
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Count 'd' digit positive integers with 0 as a digit
- Pairs whose concatenation contain all digits
- Ways to write N as sum of two or more positive integers | Set-2
- Find K distinct positive odd integers with sum N
- Number of ways in which N can be represented as the sum of two positive integers
- Find n positive integers that satisfy the given equations
- Represent (2 / N) as the sum of three distinct positive integers of the form (1 / m)
- Check whether product of integers from a to b is positive , negative or zero
- Find all the possible remainders when N is divided by all positive integers from 1 to N+1
- Print all possible ways to write N as sum of two or more positive integers
- Maximum number of distinct positive integers that can be used to represent N
- Check whether a number can be represented as sum of K distinct positive integers
- Number of arrays of size N whose elements are positive integers and sum is K
- Print first k digits of 1/n where n is a positive integer
- Minimum Possible sum of digits in a positive multiple of N
- Find the smallest positive number which can not be represented by given digits

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.