Computer >> 컴퓨터 >  >> 프로그램 작성 >> C++

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

결론

튜토리얼에서 질문이 있는 경우 댓글 섹션에 언급하세요.