# 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 sum and product will be the same. So, return that number.
- Else,
- Find the sum of digits repeatedly using
**Method 2**of Finding sum of digits of a number until sum becomes single digit. - And, Find the product of digits repeatedly using
**Method 1**of Finding sum of digits of a number until sum becomes single digit.

- Find the sum of digits repeatedly using
- Return maximum of both.

Below is the implementation of above approach:

## C++

`// CPP implementation of above approach ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to sum the digits until it ` ` ` `// becomes a single digit ` ` ` `long` `repeatedSum(` `long` `n) ` ` ` `{ ` ` ` `if` `(n == 0) ` ` ` `return` `0; ` ` ` `return` `(n % 9 == 0) ? 9 : (n % 9); ` ` ` `} ` ` ` ` ` `// Function to product the digits until it ` ` ` `// becomes a single digit ` ` ` `long` `repeatedProduct(` `long` `n) ` ` ` `{ ` ` ` `long` `prod = 1; ` ` ` ` ` `// Loop to do sum while ` ` ` `// sum is not less than ` ` ` `// or equal to 9 ` ` ` `while` `(n > 0 || prod > 9) { ` ` ` `if` `(n == 0) { ` ` ` `n = prod; ` ` ` `prod = 1; ` ` ` `} ` ` ` `prod *= n % 10; ` ` ` `n /= 10; ` ` ` `} ` ` ` `return` `prod; ` ` ` `} ` ` ` ` ` `// Function to find the maximum among ` ` ` `// repeated sum and repeated product ` ` ` `long` `maxSumProduct(` `long` `N) ` ` ` `{ ` ` ` ` ` `if` `(N < 10) ` ` ` `return` `N; ` ` ` ` ` `return` `max(repeatedSum(N), repeatedProduct(N)); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `int` `main() ` ` ` `{ ` ` ` ` ` `long` `n = 631; ` ` ` `cout << maxSumProduct(n)<<endl; ` ` ` `return` `0; ` ` ` `} ` `// This code is contributed by mits ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of above approach ` `import` `java.util.*; ` `import` `java.lang.*; ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to sum the digits until it ` ` ` `// becomes a single digit ` ` ` `public` `static` `long` `repeatedSum(` `long` `n) ` ` ` `{ ` ` ` `if` `(n == ` `0` `) ` ` ` `return` `0` `; ` ` ` `return` `(n % ` `9` `== ` `0` `) ? ` `9` `: (n % ` `9` `); ` ` ` `} ` ` ` ` ` `// Function to product the digits until it ` ` ` `// becomes a single digit ` ` ` `public` `static` `long` `repeatedProduct(` `long` `n) ` ` ` `{ ` ` ` `long` `prod = ` `1` `; ` ` ` ` ` `// Loop to do sum while ` ` ` `// sum is not less than ` ` ` `// or equal to 9 ` ` ` `while` `(n > ` `0` `|| prod > ` `9` `) { ` ` ` `if` `(n == ` `0` `) { ` ` ` `n = prod; ` ` ` `prod = ` `1` `; ` ` ` `} ` ` ` `prod *= n % ` `10` `; ` ` ` `n /= ` `10` `; ` ` ` `} ` ` ` `return` `prod; ` ` ` `} ` ` ` ` ` `// Function to find the maximum among ` ` ` `// repeated sum and repeated product ` ` ` `public` `static` `long` `maxSumProduct(` `long` `N) ` ` ` `{ ` ` ` ` ` `if` `(N < ` `10` `) ` ` ` `return` `N; ` ` ` ` ` `return` `Math.max(repeatedSum(N), repeatedProduct(N)); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` ` ` `long` `n = ` `631` `; ` ` ` `System.out.println(maxSumProduct(n)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 implementation of above approach ` ` ` `# Function to sum the digits until ` `# it becomes a single digit ` `def` `repeatedSum(n): ` ` ` `if` `(n ` `=` `=` `0` `): ` ` ` `return` `0` ` ` `return` `9` `if` `(n ` `%` `9` `=` `=` `0` `) ` `else` `(n ` `%` `9` `) ` ` ` `# Function to product the digits ` `# until it becomes a single digit ` `def` `repeatedProduct(n): ` ` ` `prod ` `=` `1` ` ` ` ` `# Loop to do sum while ` ` ` `# sum is not less than ` ` ` `# or equal to 9 ` ` ` `while` `(n > ` `0` `or` `prod > ` `9` `) : ` ` ` `if` `(n ` `=` `=` `0` `) : ` ` ` `n ` `=` `prod ` ` ` `prod ` `=` `1` ` ` ` ` `prod ` `*` `=` `n ` `%` `10` ` ` `n ` `/` `/` `=` `10` ` ` ` ` `return` `prod ` ` ` `# Function to find the maximum among ` `# repeated sum and repeated product ` `def` `maxSumProduct(N): ` ` ` ` ` `if` `(N < ` `10` `): ` ` ` `return` `N ` ` ` ` ` `return` `max` `(repeatedSum(N), ` ` ` `repeatedProduct(N)) ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `n ` `=` `631` ` ` `print` `(maxSumProduct(n)) ` ` ` `# This code is contributed ` `# by ChitraNayal ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of ` `// above approach ` `using` `System; ` `class` `GFG ` `{ ` ` ` `// Function to sum the digits ` `// until it becomes a single digit ` `public` `static` `long` `repeatedSum(` `long` `n) ` `{ ` ` ` `if` `(n == 0) ` ` ` `return` `0; ` ` ` `return` `(n % 9 == 0) ? ` ` ` `9 : (n % 9); ` `} ` ` ` `// Function to product the digits ` `// until it becomes a single digit ` `public` `static` `long` `repeatedProduct(` `long` `n) ` `{ ` ` ` `long` `prod = 1; ` ` ` ` ` `// Loop to do sum while ` ` ` `// sum is not less than ` ` ` `// or equal to 9 ` ` ` `while` `(n > 0 || prod > 9) ` ` ` `{ ` ` ` `if` `(n == 0) ` ` ` `{ ` ` ` `n = prod; ` ` ` `prod = 1; ` ` ` `} ` ` ` `prod *= n % 10; ` ` ` `n /= 10; ` ` ` `} ` ` ` `return` `prod; ` `} ` ` ` `// Function to find the maximum among ` `// repeated sum and repeated product ` `public` `static` `long` `maxSumProduct(` `long` `N) ` `{ ` ` ` ` ` `if` `(N < 10) ` ` ` `return` `N; ` ` ` ` ` `return` `Math.Max(repeatedSum(N), ` ` ` `repeatedProduct(N)); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `long` `n = 631; ` ` ` `Console.WriteLine(maxSumProduct(n)); ` `} ` `} ` ` ` `// This code is contributed ` `// by inder_verma ` |

*chevron_right*

*filter_none*

**Output:**

8

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Numbers less than N that are perfect cubes and the sum of their digits reduced to a single digit is 1
- Finding sum of digits of a number until sum becomes single digit
- Check whether a number can be expressed as a product of single digit numbers
- Reduce number to a single digit by subtracting adjacent digits repeatedly
- Maximum number with same digit factorial product
- Sum of Digits in a^n till a single digit
- Maximum sum and product of the M consecutive digits in a number
- Find the number in a range having maximum product of the digits
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Count positive integers with 0 as a digit and maximum 'd' digits
- Find the maximum sum of digits of the product of two numbers
- Product of given N fractions in reduced form
- Find maximum product of digits among numbers less than or equal to N
- Number of times a number can be replaced by the sum of its digits until it only contains one digit
- Check if given number contains a digit which is the average of all other digits
- Smallest N digit number with none of its digits as its divisor
- Find a N-digit number such that it is not divisible by any of its digits
- Find a N-digit number such that it is not divisible by any of its digits
- Number formed by adding product of its max and min digit K times
- Check if the product of digit sum and its reverse equals the number or not

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.