Open In App

Count of digits after concatenation of first N positive integers

Given a positive integer N, the task is to find the total number of digits in the concatenation of the first N positive integers.
Examples: 
 

Input: N = 10 
Output: 11 
Explanation: 
The number formed is 12345678910. 
Hence, the total number of digits = 11
Input: N = 20 
Output: 31 
Explanation: 
The number formed is 1234567891011121314151617181920 
Hence, the total number of digits = 31 
 



 

Approach: Lets make an observation with the examples. 
 



Below is the implementation of the above approach: 
 




// C++ program to find the number of
// digits after concatenating the
// first N positive integers
 
#include <iostream>
#include <math.h>
using namespace std;
 
// Function to find the number of
// digits after concatenating the
// first N positive integers
void numberOfDigits(int N)
{
    int nod = floor(log10(N) + 1);
    int toDecrease
        = (pow(10, nod) - 1) / 9;
    cout << (N + 1) * nod - toDecrease
         << endl;
}
 
// Driver code
int main()
{
    int N = 13;
    numberOfDigits(N);
 
    return 0;
}




// Java program to find the number of
// digits after concatenating the
// first N positive integers
class GFG{
 
// Function to find the number of
// digits after concatenating the
// first N positive integers
static void numberOfDigits(int N)
{
    int nod = (int)Math.floor(Math.log10(N) + 1);
    int toDecrease = (int)(Math.pow(10, nod) - 1) / 9;
     
    System.out.print((N + 1) * nod - toDecrease);
}
 
// Driver code
public static void main(String[] args)
{
    int N = 13;
    numberOfDigits(N);
}
}
 
// This code is contributed by shivanisinghss2110




# Python3 program to find the number
# of digits after concatenating the
# first N positive integers
from math import log10, floor
 
# Function to find the number of
# digits after concatenating the
# first N positive integers
def numberOfDigits(N):
     
    nod = floor(log10(N) + 1);
    toDecrease = (pow(10, nod) - 1) // 9
     
    print((N + 1) * nod - toDecrease)
 
# Driver code
if __name__ == '__main__':
     
    N = 13
     
    numberOfDigits(N)
     
# This code is contributed by mohit kumar 29




// C# program to find the number of
// digits after concatenating the
// first N positive integers
using System;
class GFG{
 
// Function to find the number of
// digits after concatenating the
// first N positive integers
static void numberOfDigits(int N)
{
    int nod = (int)Math.Floor(Math.Log10(N) + 1);
    int toDecrease = (int)(Math.Pow(10, nod) - 1) / 9;
     
    Console.Write((N + 1) * nod - toDecrease);
}
 
// Driver code
public static void Main()
{
    int N = 13;
    numberOfDigits(N);
}
}
 
// This code is contributed by Nidhi_Biet




<script>
 
// JavaScript program to implement
// the above approach
 
// Function to find the number of
// digits after concatenating the
// first N positive integers
function numberOfDigits(N)
{
    let nod = Math.floor(Math.log10(N) + 1);
    let toDecrease = (Math.pow(10, nod) - 1) / 9;
       
    document.write((N + 1) * nod - toDecrease);
}
 
// Driver code
 
    let N = 13;
    numberOfDigits(N);
  
 // This code is contributed by sanjoy_62.
</script>

Output: 
17

 

Time Complexity: O(log10N)
Auxiliary Space: O(1)


Article Tags :