You receive an integer and can't resist multiplying its digits together. For example, if you see 356, you want to know the result of 3 * 5 * 6. Quickly write a program to help yourself, who is going crazy over multiplying digits!
Sample Inputs/Outputs
Sample Input(s) | Sample Output(s) |
---|---|
On the first line, there is a number T indicating how many sets of test data there are. Next, there are T numbers n where 0 ≤ n < 2147483648. | Print the result of multiplying all digits. |
3 356 123 9999 | 90 6 6561 |
Thought Process
Use a string to collect the number, then use a for loop to multiply each digit together. It's important to note that the initial value of the variable storing the answer should be set to 1, not 0. This prevents continuously multiplying by 0, which would result in all answers being 0.
Sample Code-ZeroJudge A149: Multiplied
#include <iostream>
using namespace std;
int main() {
int N;
cin >> N;
for (int i = 0; i<N; i++)
{
string str;
cin >> str;
int sum = 1;
for (int j = 0; j<str.length(); j++)
{
sum *= int(str[j] - '0');
}
cout << sum << endl;
}
}
//Z.O.J. A149
//Dr. SeanXD