# Product of N with its largest odd digit

Given an integer N, the task is to find the product of N with its largest odd digit. If there is no odd digit in N then print -1 as the output.

Examples:

Input: 12345
Output: 61725
The largest odd digit in 12345 is 5 and 12345 * 5 = 61725

Input: 24068
Output: -1

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Traverse through all the digits of N and set maxOdd = -1, if current digit is odd and > maxOdd then update maxOdd = current digit.
If in the end, maxOdd = -1 then print -1 else print N * maxOdd.

Below is the implementation of the above approach:

## C++

 `// C++ program to find the product of N ` `// with its largest odd digit ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return the largest odd digit in n ` `int` `largestOddDigit(``int` `n) ` `{ ` `    ``// If all digits are even then -1 will be returned ` `    ``int` `maxOdd = -1; ` `    ``while` `(n > 0) { ` ` `  `        ``// Last digit from n ` `        ``int` `digit = n % 10; ` ` `  `        ``// If current digit is odd and > maxOdd ` `        ``if` `(digit % 2 == 1 && digit > maxOdd) ` `            ``maxOdd = digit; ` ` `  `        ``// Remove last digit ` `        ``n = n / 10; ` `    ``} ` ` `  `    ``// Return the maximum odd digit ` `    ``return` `maxOdd; ` `} ` ` `  `// Function to return the product of n ` `// with its largest odd digit ` `int` `getProduct(``int` `n) ` `{ ` `    ``int` `maxOdd = largestOddDigit(n); ` ` `  `    ``// If there are no odd digits in n ` `    ``if` `(maxOdd == -1) ` `        ``return` `-1; ` ` `  `    ``// Product of n with its largest odd digit ` `    ``return` `(n * maxOdd); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `n = 12345; ` `    ``cout << getProduct(n); ` `    ``return` `0; ` `} `

## Java

 `// Java program to find the product of N ` `// with its largest odd digit ` ` `  `class` `GFG ` `{ ` `     `  `// Function to return the largest ` `// odd digit in n ` `static` `int` `largestOddDigit(``int` `n) ` `{ ` `    ``// If all digits are even then -1  ` `    ``// will be returned ` `    ``int` `maxOdd = -``1``; ` `    ``while` `(n > ``0``) ` `    ``{ ` ` `  `        ``// Last digit from n ` `        ``int` `digit = n % ``10``; ` ` `  `        ``// If current digit is odd and > maxOdd ` `        ``if` `(digit % ``2` `== ``1` `&& digit > maxOdd) ` `            ``maxOdd = digit; ` ` `  `        ``// Remove last digit ` `        ``n = n / ``10``; ` `    ``} ` ` `  `    ``// Return the maximum odd digit ` `    ``return` `maxOdd; ` `} ` ` `  `// Function to return the product of n ` `// with its largest odd digit ` `static` `int` `getProduct(``int` `n) ` `{ ` `    ``int` `maxOdd = largestOddDigit(n); ` ` `  `    ``// If there are no odd digits in n ` `    ``if` `(maxOdd == -``1``) ` `        ``return` `-``1``; ` ` `  `    ``// Product of n with its largest odd digit ` `    ``return` `(n * maxOdd); ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `n = ``12345``; ` `    ``System.out.println(getProduct(n)); ` `} ` `} ` ` `  `// This code is contributed by chandan_jnu `

## Python3

 `# Python3 program to find the product  ` `# of N with its largest odd digit  ` ` `  `# Function to return the largest  ` `# odd digit in n  ` `def` `largestOddDigit(n) :  ` ` `  `    ``# If all digits are even then -1  ` `    ``# will be returned  ` `    ``maxOdd ``=` `-``1` `    ``while` `(n > ``0``) : ` ` `  `        ``# Last digit from n  ` `        ``digit ``=` `n ``%` `10` ` `  `        ``# If current digit is odd and > maxOdd  ` `        ``if` `(digit ``%` `2` `=``=` `1` `and` `digit > maxOdd) :  ` `            ``maxOdd ``=` `digit ` ` `  `        ``# Remove last digit  ` `        ``n ``=` `n ``/``/` `10` ` `  `    ``# Return the maximum odd digit  ` `    ``return` `maxOdd  ` ` `  `# Function to return the product  ` `# of n with its largest odd digit  ` `def` `getProduct(n) :  ` ` `  `    ``maxOdd ``=` `largestOddDigit(n) ` ` `  `    ``# If there are no odd digits in n  ` `    ``if` `(maxOdd ``=``=` `-``1``) : ` `        ``return` `-``1` ` `  `    ``# Product of n with its largest ` `    ``# odd digit  ` `    ``return` `(n ``*` `maxOdd) ` ` `  `# Driver code  ` `if` `__name__ ``=``=` `"__main__"` `: ` ` `  `    ``n ``=` `12345` `    ``print``(getProduct(n))  ` ` `  `# This code is contributed by Ryuga `

## C#

 `// C# program to find the product of N ` `// with its largest odd digit ` `using` `System; ` ` `  `class` `GFG ` `{ ` `     `  `// Function to return the largest ` `// odd digit in n ` `static` `int` `largestOddDigit(``int` `n) ` `{ ` `    ``// If all digits are even then -1  ` `    ``// will be returned ` `    ``int` `maxOdd = -1; ` `    ``while` `(n > 0) ` `    ``{ ` ` `  `        ``// Last digit from n ` `        ``int` `digit = n % 10; ` ` `  `        ``// If current digit is odd and > maxOdd ` `        ``if` `(digit % 2 == 1 && digit > maxOdd) ` `            ``maxOdd = digit; ` ` `  `        ``// Remove last digit ` `        ``n = n / 10; ` `    ``} ` ` `  `    ``// Return the maximum odd digit ` `    ``return` `maxOdd; ` `} ` ` `  `// Function to return the product of n ` `// with its largest odd digit ` `static` `int` `getProduct(``int` `n) ` `{ ` `    ``int` `maxOdd = largestOddDigit(n); ` ` `  `    ``// If there are no odd digits in n ` `    ``if` `(maxOdd == -1) ` `        ``return` `-1; ` ` `  `    ``// Product of n with its largest odd digit ` `    ``return` `(n * maxOdd); ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main() ` `{ ` `    ``int` `n = 12345; ` `    ``Console.Write(getProduct(n)); ` `} ` `} ` ` `  `// This code is contributed ` `// by Akanksha_Rai `

## PHP

 ` 0)  ` `    ``{ ` ` `  `        ``// Last digit from n ` `        ``\$digit` `= ``\$n` `% 10; ` ` `  `        ``// If current digit is odd and > maxOdd ` `        ``if` `(``\$digit` `% 2 == 1 && ``\$digit` `> ``\$maxOdd``) ` `            ``\$maxOdd` `= ``\$digit``; ` ` `  `        ``// Remove last digit ` `        ``\$n` `= ``\$n` `/ 10; ` `    ``} ` ` `  `    ``// Return the maximum odd digit ` `    ``return` `\$maxOdd``; ` `} ` ` `  `// Function to return the product  ` `// of n with its largest odd digit ` `function` `getProduct(``\$n``) ` `{ ` `    ``\$maxOdd` `= largestOddDigit(``\$n``); ` ` `  `    ``// If there are no odd digits in n ` `    ``if` `(``\$maxOdd` `== -1) ` `        ``return` `-1; ` ` `  `    ``// Product of n with its largest ` `    ``// odd digit ` `    ``return` `(``\$n` `* ``\$maxOdd``); ` `} ` ` `  `// Driver code ` `\$n` `= 12345; ` `echo` `getProduct(``\$n``); ` ` `  `// This code is contributed  ` `// by Akanksha Rai ` `?> `

Output:

```61725
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.