Given an integer **N > 0**, the task is to find the maximum product of digits among numbers less than or equal to **N**.

**Examples:**

Input:N = 390

Output:216

Maximum possible product is given by the number 389

3 * 8 * 9 = 216

Input:N = 432

Output:243

**Approach:** This problem can also be solved using the method described in this article taking lower limit as **1** and upper limit as **N**. Another method to solve this problem is by using recursion. The conditions for recursion are as follows:

- If
**N = 0**then return**1**. - If
**N < 10**then return**N**. - Otherwise, return
**max(maxProd(N / 10) * (N % 10), maxProd((N / 10) – 1) * 9**

At each step of recursion, either the last digit or 9 is taken to maximize the product of digit.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function that returns the maximum product of ` `// digits among numbers less than or equal to N ` `int` `maxProd(` `int` `N) ` `{ ` ` ` `if` `(N == 0) ` ` ` `return` `1; ` ` ` `if` `(N < 10) ` ` ` `return` `N; ` ` ` `return` `max(maxProd(N / 10) * (N % 10), ` ` ` `maxProd(N / 10 - 1) * 9); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `N = 390; ` ` ` `cout << maxProd(N); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function that returns the maximum product of ` `// digits among numbers less than or equal to N ` `static` `int` `maxProd(` `int` `N) ` `{ ` ` ` `if` `(N == ` `0` `) ` ` ` `return` `1` `; ` ` ` `if` `(N < ` `10` `) ` ` ` `return` `N; ` ` ` `return` `Math.max(maxProd(N / ` `10` `) * (N % ` `10` `), ` ` ` `maxProd(N / ` `10` `- ` `1` `) * ` `9` `); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main (String[] args) ` `{ ` ` ` `int` `N = ` `390` `; ` ` ` `System.out.println (maxProd(N)); ` `} ` `} ` ` ` `// This code is contributed by ajit. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function that returns the maximum product of ` `# digits among numbers less than or equal to N ` `def` `maxProd(N): ` ` ` ` ` `if` `(N ` `=` `=` `0` `): ` ` ` `return` `1` ` ` `if` `(N < ` `10` `): ` ` ` `return` `N ` ` ` `return` `max` `(maxProd(N ` `/` `/` `10` `) ` `*` `(N ` `%` `10` `), ` ` ` `maxProd(N ` `/` `/` `10` `-` `1` `) ` `*` `9` `) ` ` ` `# Driver code ` `N ` `=` `390` `print` `(maxProd(N)) ` ` ` `# This code is contributed by mohit kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function that returns the maximum product of ` `// digits among numbers less than or equal to N ` `static` `int` `maxProd(` `int` `N) ` `{ ` ` ` `if` `(N == 0) ` ` ` `return` `1; ` ` ` `if` `(N < 10) ` ` ` `return` `N; ` ` ` `return` `Math.Max(maxProd(N / 10) * (N % 10), ` ` ` `maxProd(N / 10 - 1) * 9); ` `} ` ` ` `// Driver code ` `static` `public` `void` `Main () ` `{ ` ` ` `int` `N = 390; ` ` ` `Console.WriteLine(maxProd(N)); ` `} ` `} ` ` ` `// This code is contributed by Tushil.. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// Function that returns the maximum product of ` `// digits among numbers less than or equal to N ` `function` `maxProd(` `$N` `) ` `{ ` ` ` `if` `(` `$N` `== 0) ` ` ` `return` `1; ` ` ` `if` `(` `$N` `< 10) ` ` ` `return` `$N` `; ` ` ` `return` `max(maxProd((int)(` `$N` `/ 10)) * (` `$N` `% 10), ` ` ` `maxProd((int)(` `$N` `/ 10) - 1) * 9); ` `} ` ` ` `// Driver code ` `$N` `= 390; ` `echo` `maxProd(` `$N` `); ` ` ` `// This code is contributed by Akanksha Rai ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

216

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:

- Find the number of positive integers less than or equal to N that have an odd number of digits
- Find all possible subarrays having product less than or equal to K
- Numbers less than N which are product of exactly two distinct prime numbers
- Greatest number less than equal to B that can be formed from the digits of A
- Highest and Smallest power of K less than and greater than equal to N respectively
- Find three integers less than or equal to N such that their LCM is maximum
- Find all factorial numbers less than or equal to n
- Number less than equals to N with maximum product of prime factors
- Largest number with maximum trailing nines which is less than N and greater than N-D
- Print all numbers less than N with at-most 2 unique digits
- Recursive program to print all numbers less than N which consist of digits 1 or 3 only
- Numbers less than N that are perfect cubes and the sum of their digits reduced to a single digit is 1
- Count numbers less than N containing digits from the given set : Digit DP
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Print all Semi-Prime Numbers less than or equal to N
- Sum of Semi-Prime Numbers less than or equal to N
- Print all Strong numbers less than or equal to N
- Find unique pairs such that each element is less than or equal to N
- Find Largest Special Prime which is less than or equal to a given number
- Find Multiples of 2 or 3 or 5 less than or equal to N

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.