Maximum Value – Add or Product

The program must accept N integers as the input. For each integer X among the given N integers, the program must print the maximum between the sum of digits in X and the product of digits in X.

Boundary Condition(s):
1 <= N <= 1000
1 <= Each integer value <= 10^5

Input Format:
The first line contains N.
The second line contains N integer values separated by a space.

Output Format:
The first line contains N integer values separated by a space.

Example Input/Output 1:
Input:
5
140 34 61 10 59

Output:
5 12 7 1 45

Explanation:
Here N=5 and the given 5 integers are 14034611059.
1st integer 140: (1+4+0) > (1*4*0) = 5 is the maximum.
2nd integer 34: (3+4) < (3*4) = 12 is the maximum.
3rd integer 61: (6+1) > (6*1) = 7 is the maximum.
4th integer 10: (1+0) > (1*0) = 1 is the maximum.
5th integer 59: (5+9) < (5*9) = 45 is the maximum.

Example Input/Output 2:
Input:
4
99 91 90001 444

Output:
81 10 10 64

n=int(input())
l=list(input().split())

for i in l:
    s,p=0,1
    for j in i:
        s+=int(j)
        p*=int(j)
    print(max(s,p),end=" ")
#include<stdio.h>
#include<stdlib.h>

int main()
{
    int n,val;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&val);
        int sum=0,prod=1;
        while(val>0)
        {
            sum+=val%10;
            prod*=val%10;
            val/=10;
        }
        printf("%d ",sum>prod?sum:prod);
    }

}
#include <bits/stdc++.h>

using namespace std;

int main(int argc, char** argv)
{
    int n;cin>>n;
    while(n--)
    {
        int x,m=1,a=0;
        cin>>x;
        while(x)
        {
            m*=(x%10);
            a+=(x%10);
            x/=10;
        }
        if(m>a)cout<<m<<" ";
        else cout<<a<<" ";
    }
}

import java.util.*;
public class Hello {

    public static void main(String[] args) {
//Your Code Here
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
    int num = sc.nextInt();
    int numC = num;
    long sum = 0, product = 1;
    while (num > 0) {
        sum += num % 10;
        product *= num % 10;
        num /= 10;
    }
    System.out.print(Math.max(sum, product) + " ");
}
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *

More posts. You may also be interested in.