
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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