lundi 21 octobre 2019

How to find the product of vector elements? [duplicate]

This question already has an answer here:

I have the following vector values: [2, 3, 7].

I want to output the product of the vector, as in 2*3*7 = 42.

I wrote some code for it but it doesn't appear to be working. I am new to C++, so I am not sure how to get the product of the values in a vector given any numeric vector of any size.

#include <bits/stdc++.h>

int main()
{
    int n;
    cin >> n;
    vector<int> vec;
    while (n--) 
    {
        int temp;
        cin >> temp;
        vec.push_back(temp);
    }
    int total = 1;
    total *= vec;
    cout << vec << endl;
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire