Maximum of sum and product of digits until number is reduced to a single digit
Given a number N, the task is to print the maximum between the sum and multiplication of the digits of the given number until the number is reduced to a single digit.
Note: Sum and multiplication of digits to be done until the number is reduced to a single digit.
Let’s take an example where N = 19,
19 breaks into 1+9=10 then 10 breaks into 1+0=1. 1 is a single digit sum.
Also, 19 breaks into 1*9 = 9. 9 is a single digit multiplication.
So, output is 9 i.e. maximum of 9 and 1.
Input: N = 631
Output: 8
Input: 110
Output: 2
Approach:
- Check if a number is less than 10 then the sum and product will be the same. So, return that number.
- Else,
- Return the maximum of both.
Below is the implementation of above approach:
C++
#include<bits/stdc++.h>
using namespace std;
long repeatedSum( long n)
{
if (n == 0)
return 0;
return (n % 9 == 0) ? 9 : (n % 9);
}
long repeatedProduct( long n)
{
long prod = 1;
while (n > 0 || prod > 9) {
if (n == 0) {
n = prod;
prod = 1;
}
prod *= n % 10;
n /= 10;
}
return prod;
}
long maxSumProduct( long N)
{
if (N < 10)
return N;
return max(repeatedSum(N), repeatedProduct(N));
}
int main()
{
long n = 631;
cout << maxSumProduct(n)<<endl;
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
public static long repeatedSum( long n)
{
if (n == 0 )
return 0 ;
return (n % 9 == 0 ) ? 9 : (n % 9 );
}
public static long repeatedProduct( long n)
{
long prod = 1 ;
while (n > 0 || prod > 9 ) {
if (n == 0 ) {
n = prod;
prod = 1 ;
}
prod *= n % 10 ;
n /= 10 ;
}
return prod;
}
public static long maxSumProduct( long N)
{
if (N < 10 )
return N;
return Math.max(repeatedSum(N), repeatedProduct(N));
}
public static void main(String[] args)
{
long n = 631 ;
System.out.println(maxSumProduct(n));
}
}
|
Python3
def repeatedSum(n):
if (n = = 0 ):
return 0
return 9 if (n % 9 = = 0 ) else (n % 9 )
def repeatedProduct(n):
prod = 1
while (n > 0 or prod > 9 ) :
if (n = = 0 ) :
n = prod
prod = 1
prod * = n % 10
n / / = 10
return prod
def maxSumProduct(N):
if (N < 10 ):
return N
return max (repeatedSum(N),
repeatedProduct(N))
if __name__ = = "__main__" :
n = 631
print (maxSumProduct(n))
|
C#
using System;
class GFG
{
public static long repeatedSum( long n)
{
if (n == 0)
return 0;
return (n % 9 == 0) ?
9 : (n % 9);
}
public static long repeatedProduct( long n)
{
long prod = 1;
while (n > 0 || prod > 9)
{
if (n == 0)
{
n = prod;
prod = 1;
}
prod *= n % 10;
n /= 10;
}
return prod;
}
public static long maxSumProduct( long N)
{
if (N < 10)
return N;
return Math.Max(repeatedSum(N),
repeatedProduct(N));
}
public static void Main()
{
long n = 631;
Console.WriteLine(maxSumProduct(n));
}
}
|
Javascript
<script>
function repeatedSum(n) {
if (n == 0)
return 0;
return (n % 9 == 0) ? 9 : (n % 9);
}
function repeatedProduct(n) {
var prod = 1;
while (n > 0 || prod > 9) {
if (n == 0) {
n = prod;
prod = 1;
}
prod *= n % 10;
n = parseInt(n/10);
}
return prod;
}
function maxSumProduct(N) {
if (N < 10)
return N;
return Math.max(repeatedSum(N), repeatedProduct(N));
}
var n = 631;
document.write(maxSumProduct(n));
</script>
|
Time Complexity: O(log10n)
Auxiliary Space: O(1)
Last Updated :
24 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...