# Count total number of digits from 1 to n

Given a number n, count the total number of digits required to write all numbers from 1 to n.

**Examples:**

Input : 13 Output : 17 Numbers from 1 to 13 are 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13. So 1 - 9 require 9 digits and 10 - 13 require 8 digits. Hence 9 + 8 = 17 digits are required. Input : 4 Output : 4 Numbers are 1, 2, 3, 4 . Hence 4 digits are required.

To calculate the number of digits, we have to calculate **the total number of digits required to write at ones, tens, hundredths …. places of the number **. Consider n = 13, so digits at ones place are 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3 and digits at tens place are 1, 1, 1, 1 . So, total ones place digits from 1 to 13 is basically 13 ( 13 – 0 ) and tens place digits is 4 ( 13 – 9 ) . Lets take another example n = 234, so digits at unit 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 . Digits at tens place are 234 – 9 = 225 as from 1 to 234 only 1 – 9 are single digit numbers . And lastly at hundredths place digits are 234 – 99 = 135 as only 1 – 99 are two digit numbers . Hence, total number of digits we have to write are 234 ( 234 – 1 + 1 ) + 225 ( 234 – 10 + 1 ) + 135 ( 234 – 100 + 1 ) = 594 . So, basically we have to **decrease 0, 9, 99, 999 … from n to get the number of digits at ones, tens, hundredths, thousandths … places and sum them to get the required result** .

Below is implementation of this approach .

## C/C++

`// C++ program to count total number of digits ` `// we have to write from 1 to n ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `int` `totalDigits(` `int` `n) ` `{ ` ` ` `// number_of_digits store total ` ` ` `// digits we have to write ` ` ` `int` `number_of_digits = 0; ` ` ` ` ` `// In the loop we are decreasing ` ` ` `// 0, 9, 99 ... from n till ` ` ` `// ( n - i + 1 ) is greater than 0 ` ` ` `// and sum them to number_of_digits ` ` ` `// to get the required sum ` ` ` `for` `(` `int` `i = 1; i <= n; i *= 10) ` ` ` `number_of_digits += (n - i + 1); ` ` ` ` ` `return` `number_of_digits; ` `} ` ` ` `// Driver code to test above function ` `int` `main() ` `{ ` ` ` `int` `n = 13; ` ` ` `cout << totalDigits(n) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count total number of digits ` `// we have to write from 1 to n ` ` ` `public` `class` `GFG { ` ` ` `static` `int` `totalDigits(` `int` `n) ` ` ` `{ ` ` ` `// number_of_digits store total ` ` ` `// digits we have to write ` ` ` `int` `number_of_digits = ` `0` `; ` ` ` ` ` `// In the loop we are decreasing ` ` ` `// 0, 9, 99 ... from n till ` ` ` `// ( n - i + 1 ) is greater than 0 ` ` ` `// and sum them to number_of_digits ` ` ` `// to get the required sum ` ` ` `for` `(` `int` `i = ` `1` `; i <= n; i *= ` `10` `) ` ` ` `number_of_digits += (n - i + ` `1` `); ` ` ` ` ` `return` `number_of_digits; ` ` ` `} ` ` ` ` ` `// Driver Method ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `n = ` `13` `; ` ` ` `System.out.println(totalDigits(n)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python 3

`# Python program to count total number ` `# of digits we have to write from 1 to n ` ` ` `def` `totalDigits(n): ` ` ` ` ` `# number_of_digits store total ` ` ` `# digits we have to write ` ` ` `number_of_digits ` `=` `0` `; ` ` ` ` ` `# In the loop we are decreasing ` ` ` `# 0, 9, 99 ... from n till ` ` ` `#( n - i + 1 ) is greater than 0 ` ` ` `# and sum them to number_of_digits ` ` ` `# to get the required sum ` ` ` `for` `i ` `in` `range` `(` `1` `, n, ` `10` `): ` ` ` `number_of_digits ` `=` `(number_of_digits ` `+` ` ` `(n ` `-` `i ` `+` `1` `)); ` ` ` ` ` `return` `number_of_digits; ` ` ` ` ` `# Driver code ` `n ` `=` `13` `; ` `s ` `=` `totalDigits(n) ` `+` `1` `; ` `print` `(s); ` ` ` `# This code is contributed ` `# by Shivi_Aggarwal ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count total number of ` `// digits we have to write from 1 to n ` `using` `System; ` ` ` `public` `class` `GFG { ` ` ` ` ` `static` `int` `totalDigits(` `int` `n) ` ` ` `{ ` ` ` ` ` `// number_of_digits store total ` ` ` `// digits we have to write ` ` ` `int` `number_of_digits = 0; ` ` ` ` ` `// In the loop we are decreasing ` ` ` `// 0, 9, 99 ... from n till ` ` ` `// ( n - i + 1 ) is greater than 0 ` ` ` `// and sum them to number_of_digits ` ` ` `// to get the required sum ` ` ` `for` `(` `int` `i = 1; i <= n; i *= 10) ` ` ` `number_of_digits += (n - i + 1); ` ` ` ` ` `return` `number_of_digits; ` ` ` `} ` ` ` ` ` `// Driver Method ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 13; ` ` ` ` ` `Console.WriteLine(totalDigits(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to count ` `// total number of digits ` `// we have to write from ` `// 1 to n ` ` ` `// Function that return ` `// total number of digits ` `function` `totalDigits(` `$n` `) ` `{ ` ` ` ` ` `// number_of_digits store total ` ` ` `// digits we have to write ` ` ` `$number_of_digits` `= 0; ` ` ` ` ` `// In the loop we are decreasing ` ` ` `// 0, 9, 99 ... from n till ` ` ` `// ( n - i + 1 ) is greater than 0 ` ` ` `// and sum them to number_of_digits ` ` ` `// to get the required sum ` ` ` `for` `(` `$i` `= 1; ` `$i` `<= ` `$n` `; ` `$i` `*= 10) ` ` ` `$number_of_digits` `+= (` `$n` `- ` `$i` `+ 1); ` ` ` ` ` `return` `$number_of_digits` `; ` `} ` ` ` ` ` `// Driver Code ` ` ` `$n` `= 13; ` ` ` `echo` `totalDigits(` `$n` `); ` ` ` `// This code is contributed by vt_m. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

17

**Time Complexity : O(Logn)**

This article is contributed by **Surya Priy**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Count total number of N digit numbers such that the difference between sum of even and odd digits is 1
- Find the total Number of Digits in (N!)N
- Total number of non-decreasing numbers with n digits
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Count total number of even sum sequences
- Count the total number of squares that can be visited by Bishop in one move
- Count number of digits after decimal on dividing a number
- Find count of digits in a number that divide the number
- Count digits in given number N which divide N
- Count of Numbers in Range where the number does not contain more than K non zero digits
- Count Numbers with N digits which consists of even number of 0’s
- Count Numbers with N digits which consists of odd number of 0's
- Count ways to spell a number with repeated digits
- Count the number of digits of palindrome numbers in an array