 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
Advertisements
 