
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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 Articles
- Python program to count total set bits in all number from 1 to n.
- Count digits in given number N which divide N in C++
- Total number of non-decreasing numbers with n digits
- Queries to count the number of unordered co-prime pairs from 1 to N in C++
- Program to count number of stepping numbers of n digits in python
- Find the number of integers from 1 to n which contains digits 0’s and 1’s only in C++
- C++ code to find total number of digits in special numbers
- Find count of Almost Prime numbers from 1 to N in C++
- Count number of bits changed after adding 1 to given N in C++
- Write a program in Python to count the number of digits in a given number N
- C++ program to count minimum number of operations needed to make number n to 1
- Compute sum of digits in all numbers from 1 to n
- C# program to count total bits in a number
- Count Numbers with N digits which consists of even number of 0's in C++
- Count Numbers with N digits which consists of odd number of 0's in C++
