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