乘陣列的 C 程式
給定一個數組 arr[n],其中 n 個元素,任務是求出該陣列中所有元素的乘積。
例如,我們有一個 arr[7] 陣列,其中 7 個元素,因此它的乘積將如下所示

示例
Input: arr[] = { 10, 20, 3, 4, 8 }
Output: 19200
Explanation: 10 x 20 x 3 x 4 x 8 = 19200
Input: arr[] = { 1, 2, 3, 4, 3, 2, 1 }
Output: 144下面使用的方法如下 −
- 獲取陣列輸入。
- 確定其大小。
- 遍歷陣列並乘以該陣列的每個元素
- 顯示結果
演算法
Start In function int prod_mat(int arr[], int n) Step 1-> Declare and initialize result = 1 Step 2-> Loop for i = 0 and i < n and i++ result = result * arr[i]; Step 3-> Return result int main() Step 1-> Declare an array arr[] step 2-> Declare a variable for size of array Step 3-> Print the result
示例
#include <stdio.h>
int prod_arr(int arr[], int n) {
int result = 1;
//Wil multiply each element and store it in result
for (int i = 0; i < n; i++)
result = result * arr[i];
return result;
}
int main() {
int arr[] = { 10, 20, 3, 4, 8 };
int n = sizeof(arr) / sizeof(arr[0]);
printf("%d", prod_arr(arr, n));
return 0;
}輸出
如果執行上面的程式碼,它將生成以下輸出 −
19200
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP