ด้วยอาร์เรย์ขององค์ประกอบจำนวนเต็มและภารกิจคือการคูณองค์ประกอบของอาร์เรย์และแสดงผล
ตัวอย่าง
Input-: arr[]={1,2,3,4,5,6,7}
Output-: 1 x 2 x 3 x 4 x 5 x 6 x 7 = 5040
Input-: arr[]={3, 4,6, 2, 7, 8, 4}
Output-: 3 x 4 x 6 x 2 x 7 x 8 x 4 = 32256 แนวทางที่ใช้ในโปรแกรมด้านล่างมีดังนี้ −
- เริ่มต้นตัวแปรชั่วคราวเพื่อเก็บผลลัพธ์สุดท้ายด้วย 1
- เริ่มการวนซ้ำจาก 0 ถึง n โดยที่ n คือขนาดของอาร์เรย์
- คูณค่าของ temp ด้วย arr[i] ต่อไปเพื่อให้ได้ผลลัพธ์สุดท้าย
- แสดงค่าของ temp ซึ่งจะเป็นค่าผลลัพธ์
ด้านล่างนี้คือตัวอย่างการคูณอินพุตและสร้างเอาต์พุตที่ต้องการ

อัลกอริทึม
Start
Step 1-> Declare function for multiplication of array elements
int multiply(int arr[], int len)
set int i,temp=1
Loop For i=0 and i<len and i++
Set temp=temp*arr[i]
End
return temp
step 2-> In main()
Declare int arr[]={1,2,3,4,5,6,7}
Set int len=sizeof(arr)/sizeof(arr[0])
Set int value = multiply(arr,len)
Print value
Stop ตัวอย่าง
#include<stdio.h>
//function for multiplication
int multiply(int arr[], int len) {
int i,temp=1;
for(i=0;i<len;i++) {
temp=temp*arr[i];
}
return temp;
}
int main() {
int arr[]={1,2,3,4,5,6,7};
int len=sizeof(arr)/sizeof(arr[0]);
int value = multiply(arr,len);
printf("value of array elements after multiplication : %d",value);
return 0;
} ผลลัพธ์
หากเราเรียกใช้โค้ดข้างต้น จะเกิดผลลัพธ์ดังต่อไปนี้
value of array elements after multiplication : 5040