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
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++
#include <bits/stdc++.h>
using namespace std;
int firstDigit( int x)
{
while (x >= 10)
x = x / 10;
return x;
}
int main()
{
cout << firstDigit(12345) << endl;
cout << firstDigit(5432) << endl;
}
|
Java
class Test {
static int firstDigit( int x)
{
while (x >= 10 )
x = x / 10 ;
return x;
}
public static void main(String args[])
{
System.out.println(firstDigit( 12345 ));
System.out.println(firstDigit( 5432 ));
}
}
|
Python3
def firstDigit(x):
while (x > = 10 ):
x = x / / 10
return x
print (firstDigit( 12345 ))
print (firstDigit( 5432 ))
|
C#
using System;
public class GFG {
static int firstDigit( int x)
{
while (x >= 10)
x = x / 10;
return x;
}
public static void Main()
{
Console.WriteLine(
firstDigit(12345));
Console.WriteLine(
firstDigit(5432));
}
}
|
PHP
<?php
function firstDigit( $x )
{
while ( $x >= 10)
$x = $x / 10;
return floor ( $x );
}
echo firstDigit(12345), "\n" ;
echo firstDigit(5432) ;
?>
|
Javascript
<script>
function firstDigit(x)
{
while (x >= 10)
x = x / 10;
return Math.floor(x);
}
document.write( firstDigit(12345)+ "<br>" );
document.write( firstDigit(5432)) ;
</script>
|
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++
#include <bits/stdc++.h>
using namespace std;
int FirstDigit( int arr[], int n)
{
double S = 0;
for ( int i = 0; i < n; i++)
S = S + log10 (arr[i] * 1.0);
double fract_S = S - floor (S);
int ans = pow (10, fract_S);
return ans;
}
int main()
{
int arr[] = { 5, 8, 3, 7 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << FirstDigit(arr, n) << endl;
return 0;
}
|
Java
class Test {
static int FirstDigit( int arr[], int n)
{
double S = 0 ;
for ( int i = 0 ; i < n; i++)
S = S + Math.log10(arr[i] * 1.0 );
double fract_S = S - Math.floor(S);
int ans = ( int )Math.pow( 10 , fract_S);
return ans;
}
public static void main(String args[])
{
int arr[] = { 5 , 8 , 3 , 7 };
System.out.println(FirstDigit(arr, arr.length));
}
}
|
Python3
import math
def FirstDigit (arr, n):
S = 0
for i in range (n):
S = S + math.log10(arr[i] * 1.0 )
fract_S = S - math.floor(S)
ans = math. pow ( 10 , fract_S)
return ans
arr = [ 5 , 8 , 3 , 7 ]
n = len (arr)
print (( int )(FirstDigit(arr, n)))
|
C#
using System;
public class GFG {
static int FirstDigit( int [] arr, int n)
{
double S = 0;
for ( int i = 0; i < n; i++)
S = S + Math.Log10(arr[i] * 1.0);
double fract_S = S - Math.Floor(S);
int ans = ( int )Math.Pow(10, fract_S);
return ans;
}
public static void Main()
{
int [] arr = { 5, 8, 3, 7 };
int n = arr.Length;
Console.WriteLine(FirstDigit(arr, n));
}
}
|
PHP
<?php
function FirstDigit( $arr , $n )
{
$S = 0;
for ( $i = 0; $i < $n ; $i ++)
$S = $S + log10( $arr [ $i ] * 1.0);
$fract_S = $S - floor ( $S );
$ans = pow(10, $fract_S );
return floor ( $ans );
}
$arr = array ( 5, 8, 3, 7 );
$n = sizeof( $arr );
echo FirstDigit( $arr , $n );
?>
|
Javascript
<script>
function FirstDigit(arr, n)
{
let S = 0;
for (let i = 0; i < n; i++)
S = S + Math.log10(arr[i] * 1.0);
let fract_S = S - Math.floor(S);
let ans = parseInt(Math.pow(10, fract_S), 10);
return ans;
}
let arr = [ 5, 8, 3, 7 ];
let n = arr.length;
document.write(FirstDigit(arr, n));
</script>
|
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.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
26 Sep, 2021
Like Article
Save Article