C++ 程序用于数组元素相乘
给定一个整数元素数组,任务是将数组的元素相乘并显示它。
示例
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
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP