Product of All Elements in an Array in C

To calculate the product of all elements in an array in C, we initialize a variable to store the product, iterate over the array using a loop, and multiply each element with the product variable. This approach ensures that all elements are multiplied together sequentially.


Examples to Calculate the Product of an Array

1. Calculating Product Using a for Loop

In this example, we use a for loop to traverse the array, multiply each element with a variable product, and store the result.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int numbers[] = {2, 3, 4, 5};
    int size = sizeof(numbers) / sizeof(numbers[0]);
    int product = 1;

    // Calculate product using for loop
    for (int i = 0; i < size; i++) {
        product *= numbers[i];
    }

    printf("Product of array elements: %d\n", product);

    return 0;
}

Explanation:

  1. We declare an integer array numbers[] with elements {2, 3, 4, 5}.
  2. The size of the array is calculated using sizeof(numbers) / sizeof(numbers[0]).
  3. We initialize product to 1 because multiplying by 0 would result in 0.
  4. A for loop iterates through the array, multiplying each element with product.
  5. Finally, we print the product value.

Output:

Product of array elements: 120

2. Calculating Product Using a while Loop

In this example, we use a while loop to calculate the product of array elements.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int numbers[] = {1, 2, 3, 4, 5};
    int size = sizeof(numbers) / sizeof(numbers[0]);
    int product = 1;
    int i = 0;

    // Calculate product using while loop
    while (i < size) {
        product *= numbers[i];
        i++;
    }

    printf("Product of array elements: %d\n", product);

    return 0;
}

Explanation:

  1. We declare an integer array numbers[] with elements {1, 2, 3, 4, 5}.
  2. The size of the array is determined using sizeof(numbers) / sizeof(numbers[0]).
  3. We initialize product to 1 and i to 0.
  4. A while loop runs until i < size, multiplying each element with product.
  5. The loop increments i after each multiplication.
  6. Finally, we print the product value.

Output:

Product of array elements: 120

3. Calculating Product Using Recursion

In this example, we use recursion to calculate the product of array elements.

main.c

</>
Copy
#include <stdio.h>

// Function to calculate product using recursion
int productRecursive(int arr[], int size) {
    if (size == 0) 
        return 1;
    return arr[size - 1] * productRecursive(arr, size - 1);
}

int main() {
    int numbers[] = {2, 3, 4, 5};
    int size = sizeof(numbers) / sizeof(numbers[0]);

    int product = productRecursive(numbers, size);
    printf("Product of array elements: %d\n", product);

    return 0;
}

Explanation:

  1. We define a recursive function productRecursive() that multiplies the last element with the recursive call of the remaining array.
  2. The base case returns 1 when size == 0 to stop recursion.
  3. In main(), we declare and initialize an array numbers[].
  4. We calculate size using sizeof().
  5. The productRecursive() function is called with the array and its size.
  6. The final product is printed.

Output:

Product of array elements: 120

Conclusion

In this tutorial, we explored different methods to calculate the product of all elements in an array:

  1. Using a for loop: Best for simple iterations.
  2. Using a while loop: Another iterative approach.
  3. Using recursion: A function calls itself to compute the product.