Number of n digit numbers that do not contain 9
Last Updated :
29 Mar, 2023
Given a number n, find how many n digit number can be formed that does not contain 9 as it’s digit.
Examples:
Input : 1
Output : 8
Explanation :
Except 9, all numbers are possible
Input : 2
Output : 72
Explanation :
Except numbers from 90 - 99 and all
two digit numbers that does not end
with 9 are possible.
Total numbers of n digit number that can be formed will be 9*10^(n-1) as except first position all digits can be filled with 10 numbers (0-9). If a digit 9 is eliminated from the list then total number of n digit number will be 8*9^(n-1).
Below is the implementation of above idea:
C++
#include <bits/stdc++.h>
using namespace std;
int totalNumber( int n)
{
return 8* pow (9, n - 1);
}
int main()
{
int n = 3;
cout << totalNumber(n);
return 0;
}
|
Java
import java.io.*;
public class GFG
{
static int totalNumber( int n)
{
return 8 * ( int )Math.pow( 9 , n - 1 );
}
static public void main (String[] args)
{
int n = 3 ;
System.out.println(totalNumber(n));
}
}
|
Python3
def totalNumber(n):
return 8 * pow ( 9 , n - 1 );
n = 3
print (totalNumber(n))
|
C#
using System;
public class GFG
{
static int totalNumber( int n)
{
return 8 * ( int )Math.Pow(9, n - 1);
}
static public void Main ()
{
int n = 3;
Console.WriteLine(totalNumber(n));
}
}
|
php
<?php
function totalNumber( $n )
{
return 8 * pow(9, $n - 1);
}
$n = 3;
print (totalNumber( $n ))
?>
|
Javascript
<script>
function totalNumber(n)
{
return 8 * Math.pow(9, n - 1);
}
let n = 3;
document.write(totalNumber(n));
</script>
|
Time Complexity: O(log n)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...