Given an integer **N**, the task is to find the count of **N-digit** numbers with all distinct digits.

**Examples:**

Input:N = 1

Output:9

1, 2, 3, 4, 5, 6, 7, 8 and 9 are the 1-digit numbers

with all distinct digits.

Input:N = 3

Output:648

**Approach:** If **N > 10** i.e. there will be atleast one digit which will be repeating hence for such cases the answer will be **0** else for the values of **N = 1, 2, 3, …, 9**, a series will be formed as **9, 81, 648, 4536, 27216, 136080, 544320, …** whose **N ^{th}** term will be

**9 * 9! / (10 – N)!**.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the factorial of n ` `int` `factorial(` `int` `n) ` `{ ` ` ` `if` `(n == 0) ` ` ` `return` `1; ` ` ` `return` `n * factorial(n - 1); ` `} ` ` ` `// Function to return the count ` `// of n-digit numbers with ` `// all distinct digits ` `int` `countNum(` `int` `n) ` `{ ` ` ` `if` `(n > 10) ` ` ` `return` `0; ` ` ` `return` `(9 * factorial(9) ` ` ` `/ factorial(10 - n)); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 3; ` ` ` ` ` `cout << countNum(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the factorial of n ` ` ` `static` `int` `factorial(` `int` `n) ` ` ` `{ ` ` ` `if` `(n == ` `0` `) ` ` ` `return` `1` `; ` ` ` `return` `n * factorial(n - ` `1` `); ` ` ` `} ` ` ` ` ` `// Function to return the count ` ` ` `// of n-digit numbers with ` ` ` `// all distinct digits ` ` ` `static` `int` `countNum(` `int` `n) ` ` ` `{ ` ` ` `if` `(n > ` `10` `) ` ` ` `return` `0` `; ` ` ` `return` `(` `9` `* factorial(` `9` `) / ` ` ` `factorial(` `10` `- n)); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String []args) ` ` ` `{ ` ` ` `int` `n = ` `3` `; ` ` ` `System.out.println(countNum(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Srathore ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the factorial of n ` `def` `factorial(n) : ` ` ` ` ` `if` `(n ` `=` `=` `0` `) : ` ` ` `return` `1` `; ` ` ` `return` `n ` `*` `factorial(n ` `-` `1` `); ` ` ` `# Function to return the count ` `# of n-digit numbers with ` `# all distinct digits ` `def` `countNum(n) : ` ` ` `if` `(n > ` `10` `) : ` ` ` `return` `0` `; ` ` ` ` ` `return` `(` `9` `*` `factorial(` `9` `) ` `/` `/` ` ` `factorial(` `10` `-` `n)); ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `n ` `=` `3` `; ` ` ` ` ` `print` `(countNum(n)); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the factorial of n ` ` ` `static` `int` `factorial(` `int` `n) ` ` ` `{ ` ` ` `if` `(n == 0) ` ` ` `return` `1; ` ` ` `return` `n * factorial(n - 1); ` ` ` `} ` ` ` ` ` `// Function to return the count ` ` ` `// of n-digit numbers with ` ` ` `// all distinct digits ` ` ` `static` `int` `countNum(` `int` `n) ` ` ` `{ ` ` ` `if` `(n > 10) ` ` ` `return` `0; ` ` ` `return` `(9 * factorial(9) / ` ` ` `factorial(10 - n)); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main(String []args) ` ` ` `{ ` ` ` `int` `n = 3; ` ` ` `Console.WriteLine(countNum(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Princi Singh ` |

*chevron_right*

*filter_none*

**Output:**

648

**Time Complexity:** O(n)

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.

## Recommended Posts:

- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Minimum digits to be removed to make either all digits or alternating digits same
- Count numbers with exactly K non-zero digits and distinct odd digit sum
- Count of distinct numbers formed by shuffling the digits of a large number N
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Count numbers from given range having odd digits at odd places and even digits at even places
- Numbers having Unique (or Distinct) digits
- Print a number strictly less than a given number such that all its digits are distinct.
- Count of distinct remainders when N is divided by all the numbers from the range [1, N]
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Check if the sum of digits of number is divisible by all of its digits
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Count numbers in range L-R that are divisible by all of its non-zero digits
- Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3
- Count different numbers possible using all the digits their frequency times
- Count of numbers with all digits same in a given range
- Count all prime numbers in a given range whose sum of digits is also prime

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.