📜  具有最大奇数位的N的乘积

📅  最后修改于: 2021-05-07 05:52:16             🧑  作者: Mango

给定整数N ,任务是找到具有最大奇数位的N的乘积。如果N中没有奇数位,则输出-1作为输出。
例子:

方法:遍历N的所有数字并设置maxOdd = -1 ,如果当前数字为奇数且> maxOdd,则更新maxOdd =当前数字
如果最后, maxOdd = -1,则打印-1,否则打印N * maxOdd
下面是上述方法的实现:

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


Javascript


输出:
61725