Count n digit numbers divisible by given number
Last Updated :
11 Oct, 2022
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 : 13
Explanation: There are nine n digit numbers that are divisible by 7. Numbers are 14, 21, 28, 35, 42, 49, …. 91, 98
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++
#include <cmath>
#include <iostream>
using namespace std;
int numberofterm( int n, int number)
{
int firstnum = pow (10, n - 1);
int lastnum = pow (10, n);
int count = 0;
for ( int i = firstnum; i < lastnum; i++)
if (i % number == 0)
count++;
return count;
}
int main()
{
int n = 3, num = 7;
cout << numberofterm(n, num) << "\n" ;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int numberofterm( int n, int number)
{
int firstnum = ( int )Math.pow( 10 , n - 1 );
int lastnum = ( int )Math.pow( 10 , n);
int count = 0 ;
for ( int i = firstnum; i < lastnum; i++)
if (i % number == 0 )
count++;
return count;
}
public static void main (String[] args)
{
int n = 3 , num = 7 ;
System.out.println(numberofterm(n, num));
}
}
|
Python3
import math
def numberofterm(n, number):
firstnum = math. pow ( 10 , n - 1 )
lastnum = math. pow ( 10 , n)
count = 0
for i in range ( int (firstnum), int (lastnum)):
if (i % number = = 0 ):
count + = 1
return count
n = 3
num = 7
print (numberofterm(n, num))
|
C#
using System;
class GFG
{
static int numberofterm( int n, int number)
{
int firstnum = ( int )Math.Pow(10, n - 1);
int lastnum = ( int )Math.Pow(10, n);
int count = 0;
for ( int i = firstnum; i < lastnum; i++)
if (i % number == 0)
count++;
return count;
}
public static void Main ()
{
int n = 3, num = 7;
Console.Write(numberofterm(n, num));
}
}
|
PHP
<?php
function numberofterm( $n , $number )
{
$firstnum = pow(10, $n - 1);
$lastnum = pow(10, $n );
$count = 0;
for ( $i = $firstnum ; $i < $lastnum ; $i ++)
if ( $i % $number == 0)
$count ++;
return $count ;
}
$n = 3;
$num = 7;
echo numberofterm( $n , $num );
?>
|
Javascript
<script>
function numberofterm(n, number)
{
let firstnum = Math.pow(10, n - 1);
let lastnum = Math.pow(10, n);
let count = 0;
for (let i = firstnum; i < lastnum; i++)
if (i % number == 0)
count++;
return count;
}
let n = 3, num = 7;
document.write(numberofterm(n, num));
</script>
|
Time Complexity: O(10n), which is exponential and bad for bigger n’s.
Auxiliary Space: O(1)
Efficient Approach: Find the first and last terms divisible, then apply the below formula
Count of divisible = (lastnumber – firstnumber)/number + 1
C++
#include <cmath>
#include <iostream>
using namespace std;
int numberofterm( int digit, int number)
{
int firstnum = pow (10, digit - 1);
int lastnum = pow (10, digit);
firstnum = (firstnum - firstnum % number) + number;
lastnum = (lastnum - lastnum % number);
return ((lastnum - firstnum) / number + 1);
}
int main()
{
int n = 3;
int number = 7;
cout << numberofterm(n, number) << "\n" ;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int numberofterm( int digit, int number)
{
int firstnum = ( int )Math.pow( 10 , digit - 1 );
int lastnum = ( int )Math.pow( 10 , digit);
firstnum = (firstnum - firstnum % number) + number;
lastnum = (lastnum - lastnum % number);
return ((lastnum - firstnum) / number + 1 );
}
public static void main (String[] args)
{
int n = 3 ;
int number = 7 ;
System.out.println(numberofterm(n, number));
}
}
|
Python3
def numberofterm(digit, number):
firstnum = pow ( 10 , digit - 1 )
lastnum = pow ( 10 , digit)
firstnum = (firstnum - firstnum % number) + number
lastnum = (lastnum - lastnum % number)
return ((lastnum - firstnum) / / number + 1 );
n = 3 ; number = 7
print (numberofterm(n, number))
|
C#
using System;
class GFG {
static int numberofterm( int digit,
int number)
{
int firstnum = ( int )Math.Pow(10,
digit - 1);
int lastnum = ( int )Math.Pow(10,
digit);
firstnum = (firstnum - firstnum
% number) + number;
lastnum = (lastnum - lastnum
% number);
return ((lastnum - firstnum)
/ number + 1);
}
public static void Main ()
{
int n = 3;
int number = 7;
Console.WriteLine(
numberofterm(n, number));
}
}
|
PHP
<?php
function numberofterm( $digit ,
$number )
{
$firstnum = pow(10, $digit - 1);
$lastnum = pow(10, $digit );
$firstnum = ( $firstnum - $firstnum %
$number ) + $number ;
$lastnum = ( $lastnum - $lastnum %
$number );
return (( $lastnum - $firstnum ) /
$number + 1);
}
$n = 3;
$number = 7;
echo (numberofterm( $n , $number ));
?>
|
Javascript
<script>
function numberofterm(digit, number)
{
let firstnum = Math.pow(10, digit - 1);
let lastnum = Math.pow(10, digit);
firstnum = (firstnum - firstnum % number) + number;
lastnum = (lastnum - lastnum % number);
return ((lastnum - firstnum) / number + 1);
}
let n = 3;
let number = 7;
document.write(numberofterm(n, number));
</script>
|
Time Complexity: O(log10n) as it is using inbuilt pow function
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...