#C6287. Product of Non-Zero Elements
Product of Non-Zero Elements
Product of Non-Zero Elements
You are given a list of integers. Your task is to compute the product of all non-zero elements of the list. In other words, if the list is \(a_1, a_2, \ldots, a_n\), then you need to compute
\(\prod_{\substack{1 \le i \le n \\ a_i \neq 0}} a_i\)
If there is no non-zero element in the list, output 1.
Note: Although the spirit of the problem encourages a solution without traditional loop constructs, you may use recursion or higher-order functions to achieve the result.
inputFormat
The input is read from stdin and consists of two lines:
- The first line contains a non-negative integer n representing the number of integers.
- The second line contains n space-separated integers.
outputFormat
Output a single integer to stdout which is the product of all non-zero elements of the list. If there are no non-zero elements, output 1.
This corresponds to the formula: \(\prod_{\substack{1 \le i \le n \\ a_i \neq 0}} a_i\) where the product is defined as 1 if the set of non-zero elements is empty.
## sample7
1 2 3 4 0 5 0
120