Answer to Question #223797 in Python for harish

Question #223797
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.

Output

The output should be a single line containing the count of the digits up to the given number.

Explanation

Given 

N = 10

From 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

sample input 3
1000
sample output3
2893




1
Expert's answer
2021-08-06T07:51:52-0400
def get_digits(num): 
    ans = 0 
    if(num < 10): 
        return num; 
    else:
        digit = 1
        next_d = num
        b = 1 
        while(next_d > 9): 
          ans += (9 * b * digit)
          b *= 10 
          next_d /= 10 
          digit+=1 
    ans += digit * (num + 1 - b) 
    return ans; 
N=int(input())
print(get_digits(N))

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS