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:
- We declare an integer array
numbers[]
with elements{2, 3, 4, 5}
. - The
size
of the array is calculated usingsizeof(numbers) / sizeof(numbers[0])
. - We initialize
product
to1
because multiplying by0
would result in0
. - A
for
loop iterates through the array, multiplying each element withproduct
. - 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:
- We declare an integer array
numbers[]
with elements{1, 2, 3, 4, 5}
. - The size of the array is determined using
sizeof(numbers) / sizeof(numbers[0])
. - We initialize
product
to1
andi
to0
. - A
while
loop runs untili < size
, multiplying each element withproduct
. - The loop increments
i
after each multiplication. - 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:
- We define a recursive function
productRecursive()
that multiplies the last element with the recursive call of the remaining array. - The base case returns
1
whensize == 0
to stop recursion. - In
main()
, we declare and initialize an arraynumbers[]
. - We calculate
size
usingsizeof()
. - The
productRecursive()
function is called with the array and its size. - 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:
- Using a
for
loop: Best for simple iterations. - Using a
while
loop: Another iterative approach. - Using recursion: A function calls itself to compute the product.