All possible numbers of N digits and base B without leading zeros - GeeksforGeeks

25 May, 2021
Given a number of digits ‘N’ and base ‘B’, the task is to count all the ‘N’ digit numbers without leading zeros that are in base ‘B’.
Examples:  
Input: N = 2, B = 2
Output: 2
All possible numbers without 
leading zeros are 10 and 11.

Input: N = 5, B = 8
Output: 28672
Approach:  
If the base is ‘B’ then every digit of the number can take any value within the range [0, B-1].
So, B‘N’ digit numbers are possible with base ‘B’ (including the numbers with leading zeros).
And, if we fix the first digit as ‘0’ then the rest of the ‘N-1’ digits can form a total of Bnumbers.
So, the total number of ‘N’ digit numbers with base ‘B’ possible without leading zeros are B– B.
Below is the implementation of the above approach: 
C++
Java
Python3
C#
PHP
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // function to count// all permutationsvoid countPermutations(int N, int B){    // count of    // all permutations    int x = pow(B, N);     // count of permutations    // with leading zeros    int y = pow(B, N - 1);     // Return the permutations    // without leading zeros    cout << x - y << "\n";} // Driver codeint main(){     int N = 6;    int B = 4;     countPermutations(N, B);     return 0;}
// Java implementation of the approach class GFG{// function to count// all permutationsstatic void countPermutations(int N, int B){    // count of    // all permutations    int x = (int)Math.pow(B, N);     // count of permutations    // with leading zeros    int y = (int)Math.pow(B, N - 1);     // Return the permutations    // without leading zeros    System.out.println(x - y);} // Driver codepublic static void main(String[] args){    int N = 6;    int B = 4;     countPermutations(N, B);}} // This code is contributed by mits
# Python3 implementation of the approach # function to count all permutationsdef countPermutations(N, B):     # count of all permutations    x = B ** N     # count of permutations    # with leading zeros    y = B ** (N - 1)     # Return the permutations    # without leading zeros    print(x - y) # Driver codeif __name__ == "__main__":     N, B = 6, 4    countPermutations(N, B) # This code is contributed by Rituraj Jain
// C# implementation of the approach using System;class GFG{// function to count// all permutationsstatic void countPermutations(int N, int B){    // count of    // all permutations    int x = (int)Math.Pow(B, N);     // count of permutations    // with leading zeros    int y = (int)Math.Pow(B, N - 1);     // Return the permutations    // without leading zeros    Console.WriteLine(x - y);} // Driver codepublic static void Main(){    int N = 6;    int B = 4;     countPermutations(N, B);}} // This code is contributed// by Akanksha Rai(Abby_akku)
<?php// PHP implementation of the approach // function to count all permutationsfunction countPermutations($N, $B){    // count of all permutations    $x = pow($B, $N);     // count of permutations    // with leading zeros    $y = pow($B, $N - 1);     // Return the permutations    // without leading zeros    echo ($x - $y), "\n";} // Driver code$N = 6;$B = 4; countPermutations($N, $B); // This code is contributed// by Sach_Code`?>
<script> // Javascript implementation of the approach // function to count// all permutationsfunction countPermutations(N, B){    // count of    // all permutations    var x = Math.pow(B, N);     // count of permutations    // with leading zeros    var y = Math.pow(B, N - 1);     // Return the permutations    // without leading zeros    document.write( x - y );} // Driver codevar N = 6;var B = 4;countPermutations(N, B); </script>
3072
 
Sach_Code
Akanksha_Rai
Mithun Kumar
rituraj_jain
rutvik_56
maths-power
number-digits
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Program to find GCD or HCF of two numbers
Sieve of Eratosthenes
Print all possible combinations of r elements in a given array of size n
The Knight's tour problem | Backtracking-1
Operators in C / C++
Program for factorial of a number
Find minimum number of coins that make a given value