# Find maximum product of digits among numbers less than or equal to N

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 = 390Output:216

Maximum possible product is given by the number 389

3 * 8 * 9 = 216Input:N = 432Output: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;` `}` |

## 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.` |

## 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` |

## 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..` |

## 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` `?>` |

## Javascript

`<script>` `// Javascript 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` `Math.max(maxProd(parseInt(N / 10)) * (N % 10),` ` ` `maxProd(parseInt(N / 10) - 1) * 9);` `}` `// Driver code` `let N = 390;` `document.write(maxProd(N));` `// This code is contributed` `// by bobby` `</script>` |

**Output:**

216