- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

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

# Count total number of digits from 1 to N in C++

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

## The approach used in the below program is as follows

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.

## Efficient Approach

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.

## Example (naive approach)

#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; }

## Output

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

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

## Example (Efficient approach)

#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; }

## Output

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

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

- 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
- Compute sum of digits in all numbers from 1 to n
- Program to count number of stepping numbers of n digits in python
- Count digits in given number N which divide N in C++
- 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++
- C++ program to count minimum number of operations needed to make number n to 1
- Find the number of integers from 1 to n which contains digits 0’s and 1’s only in C++
- Write a program in Python to count the number of digits in a given number N
- Count number of bits changed after adding 1 to given N in C++
- C++ code to find total number of digits in special numbers
- Program to count total number of set bits of all numbers in range 0 to n in Python
- How to count digits of given number? JavaScript
- Golang Program to Count the Number of Digits in a Number