Related Articles

Related Articles

Count n digit numbers divisible by given number
  • Difficulty Level : Easy
  • Last Updated : 19 Jul, 2018

Given number of digit n and a number, the task is to count all the numbers which are divisible by that number and having n digit.

Examples :

Input : n = 2, number = 7
Output : 9
There are nine n digit numbers that
are divisible by 7. Numbers are 14, 
21, 28, 35, 42, 49, .... 70.

Input : n = 3, number = 7
Output : 128

Input : n = 4, number = 4
Output : 2250

Native Approach: Traverse through all n digit numbers. For every number check for divisibility,

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// Simple CPP program to count n digit 
// divisible numbers.
#include <cmath>
#include <iostream>
using namespace std;
  
// Returns count of n digit numbers
// divisible by 'number'
int numberofterm(int n, int number)
{
    // compute the first and last term
    int firstnum = pow(10, n - 1);
    int lastnum = pow(10, n);
  
    // count total number of which having
    // n digit and divisible by number
    int count = 0;
    for (int i = firstnum; i < lastnum; i++) 
        if (i % number == 0)
            count++; 
    return count;
}
  
// Driver code
int main()
{
    int n = 3, num = 7;
    cout << numberofterm(n, num) << "\n";
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Simple Java program to count n digit 
// divisible numbers.
import java.io.*;
  
class GFG {
      
    // Returns count of n digit numbers
    // divisible by 'number'
    static int numberofterm(int n, int number)
    {
        // compute the first and last term
        int firstnum = (int)Math.pow(10, n - 1);
        int lastnum = (int)Math.pow(10, n);
      
        // count total number of which having
        // n digit and divisible by number
        int count = 0;
        for (int i = firstnum; i < lastnum; i++) 
            if (i % number == 0)
                count++; 
        return count;
    }
      
    // Driver code
    public static void main (String[] args) 
    {
        int n = 3, num = 7;
        System.out.println(numberofterm(n, num));
    }
}
  
// This code is contributed by Ajit.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Simple Python 3 program to count n digit 
# divisible numbers
  
import math
  
# Returns count of n digit 
# numbers divisible by number
def numberofterm(n, number):
  
    # compute the first and last term
    firstnum = math.pow(10, n - 1)
    lastnum = math.pow(10, n)
  
    # count total number of which having
    # n digit and divisible by number
    count = 0
    for i in range(int(firstnum), int(lastnum)): 
        if (i % number == 0):
            count += 1
    return count
  
  
# Driver code
n = 3 
num = 7
print(numberofterm(n, num))
  
# This article is contributed
# by Smitha Dinesh Semwal

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// Simple C# program to count n digit 
// divisible numbers.
using System;
  
class GFG 
{
      
    // Returns count of n digit numbers
    // divisible by 'number'
    static int numberofterm(int n, int number)
    {
        // compute the first and last term
        int firstnum = (int)Math.Pow(10, n - 1);
        int lastnum = (int)Math.Pow(10, n);
      
        // count total number of which having
        // n digit and divisible by number
        int count = 0;
        for (int i = firstnum; i < lastnum; i++) 
            if (i % number == 0)
                count++; 
        return count;
    }
      
    // Driver code
    public static void Main () 
    {
        int n = 3, num = 7;
        Console.Write(numberofterm(n, num));
    }
}
  
// This code is contributed by nitin mittal

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// Simple php program to count n digit 
// divisible numbers.
  
// Returns count of n digit numbers
// divisible by 'number'
function numberofterm($n, $number)
{
      
    // compute the first and last term
    $firstnum = pow(10, $n - 1);
    $lastnum = pow(10, $n);
  
    // count total number of which having
    // n digit and divisible by number
    $count = 0;
    for ($i = $firstnum; $i < $lastnum; $i++) 
        if ($i % $number == 0)
            $count++; 
    return $count;
}
  
    // Driver code
    $n = 3;
    $num = 7;
    echo numberofterm($n, $num);
      
// This code is contributed by mits
?>

chevron_right


Output:



128

Efficient Approach : Find the first and last terms divisible, then apply the below formula

Count of divisible = (lastnumber – firstnumber)/number + 1

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// Efficient CPP program to count n digit 
// divisible numbers.
#include <cmath>
#include <iostream>
using namespace std;
  
// find the number of term
int numberofterm(int digit, int number)
{
    // compute the first and last term
    int firstnum = pow(10, digit - 1);
    int lastnum = pow(10, digit);
  
    // first number which is divisible by given number
    firstnum = (firstnum - firstnum % number) + number;
  
    // last number which is divisible by given number
    lastnum = (lastnum - lastnum % number);
  
    // Apply the formula here
    return ((lastnum - firstnum) / number + 1);
}
  
int main()
{
    int n = 3;
    int number = 7;
    cout << numberofterm(n, number) << "\n";
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Efficient Java program to count n digit 
// divisible numbers.
import java.io.*;
  
class GFG {
      
    // find the number of term
    static int numberofterm(int digit, int number)
    {
        // compute the first and last term
        int firstnum = (int)Math.pow(10, digit - 1);
        int lastnum = (int)Math.pow(10, digit);
      
        // first number which is divisible by given number
        firstnum = (firstnum - firstnum % number) + number;
      
        // last number which is divisible by given number
        lastnum = (lastnum - lastnum % number);
      
        // Apply the formula here
        return ((lastnum - firstnum) / number + 1);
    }
  
    // Driver code
    public static void main (String[] args) 
    {
        int n = 3;
        int number = 7;
        System.out.println(numberofterm(n, number));
    }
}
  
// This code is contributed by Ajit.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Efficient Python program to  
# count n digit divisible numbers.
  
# Find the number of term
def numberofterm(digit, number):
      
    # compute the first and last term
    firstnum = pow(10, digit - 1)
    lastnum = pow(10, digit)
  
    # First number which is divisible by given number
    firstnum = (firstnum - firstnum % number) + number
  
    # last number which is divisible by given number
    lastnum = (lastnum - lastnum % number)
  
    # Apply the formula here
    return ((lastnum - firstnum) // number + 1);
  
# Driver code
n = 3; number = 7
print(numberofterm(n, number))
  
# This code is contributed by Ajit.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// Efficient C# program to count n digit 
// divisible numbers.
using System;
  
class GFG {
      
    // find the number of term
    static int numberofterm(int digit, 
                            int number)
    {
          
        // compute the first and 
        // last term
        int firstnum = (int)Math.Pow(10,
                             digit - 1);
                               
        int lastnum = (int)Math.Pow(10, 
                                digit);
      
        // first number which is divisible
        // by given number
        firstnum = (firstnum - firstnum 
                      % number) + number;
      
        // last number which is divisible
        // by given number
        lastnum = (lastnum - lastnum
                              % number);
      
        // Apply the formula here
        return ((lastnum - firstnum)
                          / number + 1);
    }
  
    // Driver code
    public static void Main () 
    {
        int n = 3;
        int number = 7;
          
        Console.WriteLine(
             numberofterm(n, number));
    }
}
  
// This code is contributed by anuj_67.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// Efficient PHP program 
// to count n digit 
// divisible numbers.
  
// find the number of term
function numberofterm($digit
                      $number)
{
    // compute the first
    // and last term
    $firstnum = pow(10, $digit - 1);
    $lastnum = pow(10, $digit);
  
    // first number which is 
    // divisible by given number
    $firstnum = ($firstnum - $firstnum
                 $number) + $number;
  
    // last number which is 
    // divisible by given number
    $lastnum = ($lastnum - $lastnum
                           $number);
  
    // Apply the formula here
    return (($lastnum - $firstnum) / 
                        $number + 1);
}
  
// Driver Code
$n = 3;
$number = 7;
echo (numberofterm($n, $number));
  
// This code is contributed by 
// Manish Shaw(manishshaw1)
?>

chevron_right


Output:

128

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :