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

結論

如果您對教程有任何疑問,請在評論區提出。

更新日期:2020 年 12 月 29 日

119 次瀏覽

開啟你的職業

透過完成課程獲得認證

開始
廣告
© . All rights reserved.