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 <bits/stdc++.h>
usingnamespacestd;
intfirstDigit(intx)
{
// Keep dividing by 10 until it is
// greater than equal to 10
while(x >= 10)
x = x / 10;
returnx;
}
// driver function
intmain()
{
cout << firstDigit(12345) << endl;
cout << firstDigit(5432) << endl;
}
Java
// Java implementation to find first digit of a
// single number
classTest {
staticintfirstDigit(intx)
{
// Keep dividing by 10 until it is
// greater than equal to 10
while(x >= 10)
x = x / 10;
returnx;
}
// Driver method
publicstaticvoidmain(String args[])
{
System.out.println(firstDigit(12345));
System.out.println(firstDigit(5432));
}
}
Python3
# Python implementation to
# find first digit of a
# single number
deffirstDigit(x):
# Keep dividing by 10 until it is
# greater than equal to 10
while(x >=10):
x =x//10
returnx
# 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
usingSystem;
publicclassGFG {
staticintfirstDigit(intx)
{
// Keep dividing by 10 until
// it is greater than equal
// to 10
while(x >= 10)
x = x / 10;
returnx;
}
// Driver method
publicstaticvoidMain()
{
Console.WriteLine(
firstDigit(12345));
Console.WriteLine(
firstDigit(5432));
}
}
// This code is contributed by Sam007.
PHP
<?php
// PHP implementation to
// find first digit of a
// single number
functionfirstDigit($x)
{
// Keep dividing by 10
// until it is greater
// than equal to 10
while($x>= 10)
$x= $x/ 10;
returnfloor($x);
}
// Driver Code
echofirstDigit(12345),"\n";
echofirstDigit(5432) ;
// This code is contributed by vishal tripathi.
?>
Javascript
<script>
// Javascript implementation to
// find first digit of a
// single number
functionfirstDigit(x)
{
// Keep dividing by 10
// until it is greater
// than equal to 10
while(x >= 10)
x = x / 10;
returnMath.floor(x);
}
// Driver Code
document.write( firstDigit(12345)+"<br>");
document.write( firstDigit(5432)) ;
// This code is contributed by Bobby
</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++
// C++ implementation of finding first digit
// of product of n numbers
#include <bits/stdc++.h>
usingnamespacestd;
// returns the first digit of product of elements of arr[]
intFirstDigit(intarr[], intn)
{
// stores the logarithm of product of elements of arr[]
doubleS = 0;
for(inti = 0; i < n; i++)
S = S + log10(arr[i] * 1.0);
// fractional(s) = s - floor(s)
doublefract_S = S - floor(S);
// ans = 10^fract_s
intans = pow(10, fract_S);
returnans;
}
// Driver function
intmain()
{
intarr[] = { 5, 8, 3, 7 };
intn = sizeof(arr) / sizeof(arr[0]);
cout << FirstDigit(arr, n) << endl;
return0;
}
Java
// Java implementation of finding first digit
// of product of n numbers
classTest {
// returns the first digit of product of elements of arr[]
staticintFirstDigit(intarr[], intn)
{
// stores the logarithm of product of elements of arr[]
doubleS = 0;
for(inti = 0; i < n; i++)
S = S + Math.log10(arr[i] * 1.0);
// fractional(s) = s - floor(s)
doublefract_S = S - Math.floor(S);
// ans = 10^fract_s
intans = (int)Math.pow(10, fract_S);
returnans;
}
// Driver method
publicstaticvoidmain(String args[])
{
intarr[] = { 5, 8, 3, 7};
System.out.println(FirstDigit(arr, arr.length));
}
}
Python3
# Python implementation of
# finding first digit
# of product of n numbers
importmath
# Returns the first digit of
# product of elements of arr[]
defFirstDigit (arr, n):
# stores the logarithm of
# product of elements of arr[]
S =0
fori inrange(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)
returnans
# 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
usingSystem;
publicclassGFG {
// returns the first digit of product
// of elements of arr[]
staticintFirstDigit(int[] arr, intn)
{
// stores the logarithm of product
// of elements of arr[]
doubleS = 0;
for(inti = 0; i < n; i++)
S = S + Math.Log10(arr[i] * 1.0);
// fractional(s) = s - floor(s)
doublefract_S = S - Math.Floor(S);
// ans = 10^fract_s
intans = (int)Math.Pow(10, fract_S);
returnans;
}
// Driver method
publicstaticvoidMain()
{
int[] arr = { 5, 8, 3, 7 };
intn = arr.Length;
Console.WriteLine(FirstDigit(arr, n));
}
}
// This code is contributed by Sam007.
PHP
<?php
// PHP implementation of
// finding first digit of
// product of n numbers
// Returns the first digit of
// product of elements of arr[]
functionFirstDigit($arr, $n)
{
// stores the logarithm of
// product of elements of arr[]
$S= 0;
for($i= 0; $i< $n; $i++)
$S= $S+ log10($arr[$i] * 1.0);
// fractional(s) = s - floor(s)
$fract_S= $S- floor($S);
// ans = 10^fract_s
$ans= pow(10, $fract_S);
returnfloor($ans);
}
// Driver Code
$arr= array( 5, 8, 3, 7 );
$n= sizeof($arr);
echoFirstDigit($arr, $n);
// This code is contributed by aj_36
?>
Javascript
<script>
// Javascript implementation of finding first
// digit of product of n numbers
// returns the first digit of product
// of elements of arr[]
functionFirstDigit(arr, n)
{
// stores the logarithm of product
// of elements of arr[]
let S = 0;
for(let i = 0; i < n; i++)
S = S + Math.log10(arr[i] * 1.0);
// fractional(s) = s - floor(s)
let fract_S = S - Math.floor(S);
// ans = 10^fract_s
let ans = parseInt(Math.pow(10, fract_S), 10);
returnans;
}
let arr = [ 5, 8, 3, 7 ];
let n = arr.length;
document.write(FirstDigit(arr, n));
</script>
Output :
8
This article is contributed by Ayush Jha. If you like zambiatek and would like to contribute, you can also write an article using write.zambiatek.com or mail your article to review-team@zambiatek.com. See your article appearing on the zambiatek 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 zambiatek!