- Related Questions & Answers
- Python program to count total set bits in all number from 1 to n.
- Total number of non-decreasing numbers with n digits
- Count digits in given number N which divide N in C++
- Compute sum of digits in all numbers from 1 to n
- Queries to count the number of unordered co-prime pairs from 1 to N in C++
- Find count of Almost Prime numbers from 1 to N in C++
- Program to count number of stepping numbers of n digits in python
- Count number of bits changed after adding 1 to given N in C++
- Find the number of integers from 1 to n which contains digits 0’s and 1’s only in C++
- C# program to count total bits in a number
- Count total bits in a number in C++
- Count of pairs from 1 to a and 1 to b whose sum is divisible by N in C++
- Count number of step required to reduce N to 1 by following certain rule in C++
- Minimum number of letters needed to make a total of n in C++.
- Sum of the digits of a number N written in all bases from 2 to N/2 in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

We are given a number N as input. The goal is to count the total number of digits between numbers 1 to N. 1 to 9 numbers require 1 digit each, 11 to 99 require 2 digits each, 100 to 999 require 3 digits each and so on.

Let us understand with examples

**Input** − N=11

**Output** − Count of total number of digits from 1 to N are: 13

**Explanation** − Numbers 1 to 9 has 1 digit each : 9 digits 10, 11 have 2 digits each. 4 digits. Total digits= 9+4=13.

**Input** − N=999

**Output** − Count of the total number of digits from 1 to N are: 2889

**Explanation **− Numbers 1 to 9 have 1 digit each : 9 digits 10 to 99 have 2 digits each. : 180 digits. 100 to 999 have 3 digits each : 2700 digits Total digits= 2700 + 180 + 9 = 2889 digits

We will use two approaches. A first naive approach using a recursive function to calculate digits in a number num. Convert passed num into a string. The length of the string is digits in num. Do this for each number bypassing current num-1 recursively.

Take a number as a positive integer.

Function total_digits(int num) take the num and returns digit in numbers between 1 to num.

To calculate digits in num, convert num to string. (to_string(num)).

The length of the string is digits in num.

If num is 1 return 1 . Else return length+ total_digits(num-1) for rest of numbers less than num.

At last we will get total digits as a result.

In this approach, we will use the logic that for each number up to N. We will traverse 10, 100, 1000 up to N. For each 10i , the number of digits is (num-i + 1).

Take a number as a positive integer.

Function total_digits(int num) take the num and returns digit in numbers between 1 to num.

Take the total count as 0 initially.

Traverse i=1 to i<=num, increment i by 10 in each iteration and add num-i+1 to count.

Return the count at the end of for loop as a result.

#include <bits/stdc++.h> using namespace std; int total_digits(int num){ string str = to_string(num); int length = str.length(); if (num == 1){ return 1; } return length + total_digits(num - 1); } int main(){ int num = 20; cout<<"Count of total number of digits from 1 to n are: "<<total_digits(num); return 0; }

If we run the above code it will generate the following output −

Count of total number of digits from 1 to n are: 31

#include <bits/stdc++.h> using namespace std; int total_digits(int num){ int count = 0; for(int i = 1; i <= num; i *= 10){ count = count + (num - i + 1); } return count; } int main(){ int num = 20; cout<<"Count of total number of digits from 1 to n are: "<<total_digits(num); return 0; }

If we run the above code it will generate the following output −

Count of total number of digits from 1 to n are: 31

Advertisements