Number of digits until N
Given an integer N, write a program that prints the count of the total number of digits between 1 and N.
Input
The input is a positive integer.
The output should be a single line containing the count of the digits up to the given number.
Given
N = 10From 1 to 9, each number contains a single digit. From 10 onwards, the numbers contain two digits.
So the output should be 9 + 2 =
11.
Sample Input 1
10
Sample Output 1
11
Sample Input 2
4
Sample Output 2
4
n = int(input())
k = 1
count = 0
while 10 ** k <= n:
count += 9 * 10 ** (k - 1) * k
k += 1
count += (n - 10 ** (k - 1) + 1) * k
print(count)
Comments
Leave a comment