C++ 中数字数组相乘的第一个数字
在本文教程中,我们将学习如何找到一个数组乘积的第一个数字。
让我们看看解决此问题的步骤。
初始化数组。
找到数组中元素的乘积。
除以该结果,直至小于 10。
打印一位数字
示例
让我们看看代码。
#include <bits/stdc++.h>
using namespace std;
int productOfArrayDigits(int arr[], int n) {
int product = 1;
for (int i = 0; i < n; i++) {
product *= arr[i];
}
return product;
}
int firstDigitOfNumber(int n) {
while (n >= 10) {
n /= 10;
}
return n;
}
int main() {
int arr[] = { 1, 2, 3, 4, 5, 6 };
cout << firstDigitOfNumber(productOfArrayDigits(arr, 6)) << endl;
return 0;
}输出
如果您运行以上代码,则将获得以下结果。
7
结论
如果您对教程有任何疑问,请在评论区提出。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP