First digit in product of an array of numbers in C++


In this tutorial, we are going to learn how to find first digit of the product of an array.

Let's see the steps to solve the problem.

  • Initialize the array.

  • Find the product of the elements in the array.

  • Divide the result until it's less than 10.

  • Print the single-digit

Example

Let's see the code.

 Live Demo

#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;
}

Output

If you run the above code, then you will get the following result.

7

Conclusion

If you have any queries in the tutorial, mention them in the comment section.

Updated on: 29-Dec-2020

110 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements