Product of N with its largest odd digit
Last Updated :
24 May, 2022
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
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++
#include <bits/stdc++.h>
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;
}
|
Java
class GFG
{
static 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;
}
static int getProduct( int n)
{
int maxOdd = largestOddDigit(n);
if (maxOdd == - 1 )
return - 1 ;
return (n * maxOdd);
}
public static void main(String[] args)
{
int n = 12345 ;
System.out.println(getProduct(n));
}
}
|
Python3
def largestOddDigit(n) :
maxOdd = - 1
while (n > 0 ) :
digit = n % 10
if (digit % 2 = = 1 and digit > maxOdd) :
maxOdd = digit
n = n / / 10
return maxOdd
def getProduct(n) :
maxOdd = largestOddDigit(n)
if (maxOdd = = - 1 ) :
return - 1
return (n * maxOdd)
if __name__ = = "__main__" :
n = 12345
print (getProduct(n))
|
C#
using System;
class GFG
{
static 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;
}
static int getProduct( int n)
{
int maxOdd = largestOddDigit(n);
if (maxOdd == -1)
return -1;
return (n * maxOdd);
}
public static void Main()
{
int n = 12345;
Console.Write(getProduct(n));
}
}
|
PHP
<?php
function largestOddDigit( $n )
{
$maxOdd = -1;
while ( $n > 0)
{
$digit = $n % 10;
if ( $digit % 2 == 1 && $digit > $maxOdd )
$maxOdd = $digit ;
$n = $n / 10;
}
return $maxOdd ;
}
function getProduct( $n )
{
$maxOdd = largestOddDigit( $n );
if ( $maxOdd == -1)
return -1;
return ( $n * $maxOdd );
}
$n = 12345;
echo getProduct( $n );
?>
|
Javascript
<script>
function largestOddDigit(n)
{
var maxOdd = -1;
while (n > 0)
{
var digit = n % 10;
if (digit % 2 == 1 && digit > maxOdd)
maxOdd = digit;
n = n / 10;
}
return maxOdd;
}
function getProduct(n)
{
var maxOdd = largestOddDigit(n);
if (maxOdd == -1)
return -1;
return (n * maxOdd);
}
var n = 12345;
document.write(getProduct(n));
</script>
|
Time Complexity: O(log10n), where n represents the given integer.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...