OneBite.Dev - Coding blog in a bite size

find the product of all elements in an array in C

Code snippet on how to find the product of all elements in an array in C

  int arrayProduct(int arr[], int length) 
  { 
    int product = 1; 
    for (int i = 0; i < length; ++i) 
        product *= arr[i]; 
  
    return product; 
  } 

This code takes in an array of integers and its length as parameters. It iterates through each element in the array and multiplies the product of all of the elements together. Each time an element is multiplied, the product updates and stores the new value. After iterating through the array and multiplying all elements, the product is finally returned. For example, if the array contains [2, 3, 4] then the product of these elements is 24.

c