multiply values in array

hello guys if we have array like

int numbers [] = {3,4,8,9,6,3,2,58,6,3,2,5};

how can we fast do smth like 3*4*8*9*6*3*2*58*6*3*2*5

also numbers[0]*numbers[1]*numbers[2] etc
all must be in one loop
You need to store the intermediate result in a seperate variable. So essentially, try:

1
2
3
4
5
int total = numbers[0];

total *= numbers[1];
total *= numbers[2];
..etc
#include <iostream>

using namespace std;


int main(){
int prod=1;
int v[]={1,2,3,4,5};
for(auto x:v){
prod *=x;
}

cout<<prod;
return 0;
}
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <algorithm>
#include <functional>

int main()
{
    int numbers [] = {3, 4, 8, 9, 6, 3, 2, 58, 6, 3, 2, 5};
    std::cout << std::accumulate(std::begin(numbers), std::end(numbers), 1, std::multiplies<int>());
}
Topic archived. No new replies allowed.