#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int product = 1;
for (int i = 0; i < 5; i++) {
product *= arr[i];
}
printf("Product of array elements: %d\n", product);
return 0;
}
- Include the standard input/output library
stdio.h
to use functions like printf
. - Define the
main
function, which is the entry point of the program. - Declare an array
arr
with a size of 5 and initialize it with values 1, 2, 3, 4, and 5. - Declare a variable
product
and initialize it with the value 1. This variable will store the product of the array elements. - Start a
for
loop that iterates from 0 to 4 (inclusive) using the variable i
as the loop counter. - Inside the loop, multiply the current element of the array
arr[i]
with the product
variable and store the result back in the product
variable. This step calculates the product of all the array elements. - After the loop ends, use the
printf
function to print the value of the product
variable, which represents the product of the array elements. - Finally, return 0 to indicate successful program execution.