
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Maximum of sum and product of digits until number is reduced to a single digit in C++
In this tutorial, we will be discussing a program to find maximum of sum and product of digits until number is reduced to a single digit
For this we will be provided with a random number. Our task is to find and print out the maximum of sum and product of the digits of the given number until it coverts to a single digit
Example
#include<bits/stdc++.h> using namespace std; //converting number to single digit by adding long repeatedSum(long n) { if (n == 0) return 0; return (n % 9 == 0) ? 9 : (n % 9); } //converting number to single digit by multiplying 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; } //finding maximum 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; }
Output
8
- Related Articles
- Finding sum of digits of a number until sum becomes single digit in C++
- C++ program to find sum of digits of a number until sum becomes single digit
- Program to find sum of digits until it is one digit number in Python
- Summing up all the digits of a number until the sum is one digit in JavaScript
- Maximum sum and product of the M consecutive digits in a number in C++
- A two-digit number is 4 times the sum of its digits and twice the product of the digits. Find the number.
- A two digit number is 4 times the sum of its digits and twice the product of its digits. Find the number.
- Digit sum upto a number of digits of a number in JavaScript
- Difference between product and sum of digits of a number in JavaScript
- Product sum difference of digits of a number in JavaScript
- What is the minimum and maximum number of digits in the sum if we add any two 3 digit number
- C program to find sum of digits of a five digit number
- Sum up a number until it becomes one digit - JavaScript
- Sum up a number until it becomes 1 digit JavaScript
- Reduce sum of digits recursively down to a one-digit number JavaScript

Advertisements