# First digit in product of an array of numbers

Given an array of ‘n’ numbers. We need to find the first digit of product of these ‘n’ numbers
Examples :

```Input  : arr[] = {5, 8, 3, 7}
Output : 8
Product of 5, 8, 3, 7 is 840
and its first  digit is 8

Input  : arr[] = {6, 7, 9}
Output : 3```

Recommended Practice

Background :
First we start from a very basic question how to find the first digit of any number x.To do this keep dividing the number until it is greater than equal to 10. After doing this the number which we will get will be first digit of x

## C++

 `// C++ implementation to find first digit of a` `// single number` `#include ` `using` `namespace` `std;`   `int` `firstDigit(``int` `x)` `{` `    ``// Keep dividing by 10 until it is` `    ``// greater than equal to 10` `    ``while` `(x >= 10)` `        ``x = x / 10;` `    ``return` `x;` `}`   `// driver function` `int` `main()` `{` `    ``cout << firstDigit(12345) << endl;` `    ``cout << firstDigit(5432) << endl;` `}`

## Java

 `// Java implementation to find first digit of a` `// single number`   `class` `Test {` `    ``static` `int` `firstDigit(``int` `x)` `    ``{` `        ``// Keep dividing by 10 until it is` `        ``// greater than equal to 10` `        ``while` `(x >= ``10``)` `            ``x = x / ``10``;` `        ``return` `x;` `    ``}`   `    ``// Driver method` `    ``public` `static` `void` `main(String args[])` `    ``{` `        ``System.out.println(firstDigit(``12345``));` `        ``System.out.println(firstDigit(``5432``));` `    ``}` `}`

## Python3

 `# Python implementation to` `# find first digit of a` `# single number`   `def` `firstDigit(x):`   `    ``# Keep dividing by 10 until it is` `    ``# greater than equal to 10` `    ``while``(x >``=` `10``):` `        ``x ``=` `x``/``/``10` `    ``return` `x`   ` `  `# driver function`   `print``(firstDigit(``12345``))` `print``(firstDigit(``5432``))`   `# This code is contributed` `# by Anant Agarwal.`

## C#

 `// C# implementation to find first` `// digit of a single number` `using` `System;`   `public` `class` `GFG {` `    `  `    ``static` `int` `firstDigit(``int` `x)` `    ``{` `        `  `        ``// Keep dividing by 10 until` `        ``// it is greater than equal` `        ``// to 10` `        ``while` `(x >= 10)` `            ``x = x / 10;` `            `  `        ``return` `x;` `    ``}`   `    ``// Driver method` `    ``public` `static` `void` `Main()` `    ``{` `        ``Console.WriteLine(` `                  ``firstDigit(12345));` `                  `  `        ``Console.WriteLine(` `                   ``firstDigit(5432));` `    ``}` `}`   `// This code is contributed by Sam007.`

## PHP

 `= 10)` `        ``\$x` `= ``\$x` `/ 10;` `    ``return` `floor``(``\$x``);` `}`   `    ``// Driver Code` `    ``echo` `firstDigit(12345),``"\n"` `;` `    ``echo` `firstDigit(5432) ;`   `// This code is contributed by vishal tripathi.` `?>`

## Javascript

 ``

Output:

```1
5```

Solution :
For an array of numbers, product can be very big and their multiplication might not fit in any typical data type. Even if you use Big int the number will very big and finding the first by direct division by 10 method will be very slow. So we need to use something different
let the numbers be ……and their product is P .P = *…..*
let S = (P) = () + ()…..+().
So we can say P =
We know that any number can be written as sum of its floor value and fractional value.
therefore P = which implies P = *
Now we can apply our above discussed method of finding first digit of a number because after dividing P by 10 until it is greater than equal to 10 we will be left only with which will be our answer. And fractional(S) can be easily calculated fractional(S) = S – floor(S).

## C++

 `// C++ implementation of finding first digit` `// of product of n numbers` `#include ` `using` `namespace` `std;`   `// returns the first digit of product of elements of arr[]` `int` `FirstDigit(``int` `arr[], ``int` `n)` `{` `    ``// stores the logarithm of product of elements of arr[]` `    ``double` `S = 0;` `    ``for` `(``int` `i = 0; i < n; i++)` `        ``S = S + ``log10``(arr[i] * 1.0);`   `    ``// fractional(s) = s - floor(s)` `    ``double` `fract_S = S - ``floor``(S);`   `    ``// ans = 10^fract_s` `    ``int` `ans = ``pow``(10, fract_S);` `    ``return` `ans;` `}`   `// Driver function` `int` `main()` `{` `    ``int` `arr[] = { 5, 8, 3, 7 };` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr[0]);` `    ``cout << FirstDigit(arr, n) << endl;` `    ``return` `0;` `}`

## Java

 `// Java implementation of finding first digit` `// of product of n numbers`   `class` `Test {` `    ``// returns the first digit of product of elements of arr[]` `    ``static` `int` `FirstDigit(``int` `arr[], ``int` `n)` `    ``{` `        ``// stores the logarithm of product of elements of arr[]` `        ``double` `S = ``0``;` `        ``for` `(``int` `i = ``0``; i < n; i++)` `            ``S = S + Math.log10(arr[i] * ``1.0``);`   `        ``// fractional(s) = s - floor(s)` `        ``double` `fract_S = S - Math.floor(S);`   `        ``// ans = 10^fract_s` `        ``int` `ans = (``int``)Math.pow(``10``, fract_S);` `        ``return` `ans;` `    ``}`   `    ``// Driver method` `    ``public` `static` `void` `main(String args[])` `    ``{` `        ``int` `arr[] = { ``5``, ``8``, ``3``, ``7` `};`   `        ``System.out.println(FirstDigit(arr, arr.length));` `    ``}` `}`

## Python3

 `# Python implementation of` `# finding first digit ` `# of product of n numbers`   `import` `math`   `# Returns the first digit of` `# product of elements of arr[]` `def` `FirstDigit (arr, n):`   `    ``# stores the logarithm of` `    ``# product of elements of arr[]` `    ``S ``=` `0` `    ``for` `i ``in` `range``(n):` `        ``S ``=` `S ``+` `math.log10(arr[i]``*``1.0``)` ` `  `    ``# fractional(s) = s - floor(s)` `    ``fract_S ``=` `S ``-` `math.floor(S)` ` `  `    ``# ans = 10 ^ fract_s` `    ``ans ``=` `math.``pow``(``10``, fract_S)` `    ``return` `ans` ` `  `# Driver function`   `arr ``=` `[``5``, ``8``, ``3``, ``7``]` `n ``=` `len``(arr)` `print``((``int``)(FirstDigit(arr, n)))` `    `  `# This code is contributed` `# by Anant Agarwal.`

## C#

 `// C# implementation of finding first` `// digit of product of n numbers` `using` `System;`   `public` `class` `GFG {` `    `  `    ``// returns the first digit of product` `    ``// of elements of arr[]` `    ``static` `int` `FirstDigit(``int``[] arr, ``int` `n)` `    ``{` `        `  `        ``// stores the logarithm of product` `        ``// of elements of arr[]` `        ``double` `S = 0;` `        `  `        ``for` `(``int` `i = 0; i < n; i++)` `            ``S = S + Math.Log10(arr[i] * 1.0);`   `        ``// fractional(s) = s - floor(s)` `        ``double` `fract_S = S - Math.Floor(S);`   `        ``// ans = 10^fract_s` `        ``int` `ans = (``int``)Math.Pow(10, fract_S);` `        `  `        ``return` `ans;` `    ``}`   `    ``// Driver method` `    ``public` `static` `void` `Main()` `    ``{` `        ``int``[] arr = { 5, 8, 3, 7 };` `        ``int` `n = arr.Length;` `        `  `        ``Console.WriteLine(FirstDigit(arr, n));` `    ``}` `}`   `// This code is contributed by Sam007.`

## PHP

 ``

## Javascript

 ``

Output :

`8`

If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.