ในบทความนี้ เราจะพูดถึงโปรแกรมเพื่อค้นหาตัวเลขตัวแรกในผลคูณขององค์ประกอบของอาร์เรย์ที่กำหนด
ตัวอย่างเช่น สมมติว่าเราได้รับอาร์เรย์
arr = {12, 5, 16} จากนั้นผลคูณขององค์ประกอบเหล่านี้จะเป็น 12*5*16 =960 ดังนั้นผลลัพธ์คือตัวเลขตัวแรกของผลิตภัณฑ์ในกรณีนี้จะเป็น 9
ตัวอย่าง
#include <bits/stdc++.h>
using namespace std;
int calc_1digit(int arr[], int x) {
long long int prod = 1;
for(int i = 0;i < x; i++) {
prod = prod*arr[i];
}
while (prod >= 10)
prod = prod / 10;
return prod;
}
int main() {
int arr[]={12,43,32,54};
cout <<"The first digit will be: " << calc_1digit(arr,4)<< endl;
} ผลลัพธ์
The first digit will be: 8