Number of digits before the decimal point in the division of two numbers
Last Updated :
22 Jun, 2022
Given two integers a and b. The task is to find the number of digits before the decimal point in a / b.
Examples:
Input: a = 100, b = 4
Output: 2
100 / 4 = 25 and number of digits in 25 = 2.
Input: a = 100000, b = 10
Output: 5
Naive approach: Divide the two numbers and then find the number of digits in the division. Take the absolute value of the division for finding the number of digits.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countDigits( int a, int b)
{
int count = 0;
int p = abs (a / b);
if (p == 0)
return 1;
while (p > 0) {
count++;
p = p / 10;
}
return count;
}
int main()
{
int a = 100;
int b = 10;
cout << countDigits(a, b);
return 0;
}
|
Java
class GFG {
static int countDigits( int a, int b)
{
int count = 0 ;
int p = Math.abs(a / b);
if (p == 0 )
return 1 ;
while (p > 0 ) {
count++;
p = p / 10 ;
}
return count;
}
public static void main(String args[])
{
int a = 100 ;
int b = 10 ;
System.out.print(countDigits(a, b));
}
}
|
Python
def countDigits(a, b):
count = 0
p = abs (a / / b)
if (p = = 0 ):
return 1
while (p > 0 ):
count = count + 1
p = p / / 10
return count
a = 100
b = 10
print (countDigits(a, b))
|
C#
using System;
class GFG {
static int countDigits( int a, int b)
{
int count = 0;
int p = Math.Abs(a / b);
if (p == 0)
return 1;
while (p > 0) {
count++;
p = p / 10;
}
return count;
}
public static void Main()
{
int a = 100;
int b = 10;
Console.Write(countDigits(a, b));
}
}
|
PHP
<?php
function countDigits( $a , $b )
{
$count = 0;
$p = abs ( $a / $b );
if ( $p == 0)
return 1;
while ( $p > 0) {
$count ++;
$p = (int)( $p / 10);
}
return $count ;
}
$a = 100;
$b = 10;
echo countDigits( $a , $b );
?>
|
Javascript
<script>
function countDigits(a, b)
{
var count = 0;
var p = Math.abs(parseInt(a / b));
if (p == 0)
return 1;
while (p > 0) {
count++;
p = parseInt(p / 10);
}
return count;
}
var a = 100;
var b = 10;
document.write(countDigits(a, b));
</script>
|
Time Complexity: O(log10(a/ b))
Auxiliary Space: O(1)
Efficient approach: To count the number of digits in a / b, we can use the formula:
floor(log10(a) – log10(b)) + 1
Here both the numbers need to be positive integers. For this we can take the absolute values of a and b.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countDigits( int a, int b)
{
return floor ( log10 ( abs (a)) - log10 ( abs (b))) + 1;
}
int main()
{
int a = 100;
int b = 10;
cout << countDigits(a, b);
return 0;
}
|
Java
class GFG {
public static int countDigits( int a, int b)
{
double digits = Math.log10(Math.abs(a))
- Math.log10(Math.abs(b)) + 1 ;
return ( int )Math.floor(digits);
}
public static void main(String[] args)
{
int a = 100 ;
int b = 10 ;
System.out.print(countDigits(a, b));
}
}
|
Python
import math
def countDigits(a, b):
return math.floor(math.log10( abs (a)) -
math.log10( abs (b))) + 1
a = 100
b = 10
print (countDigits(a, b))
|
C#
using System;
class GFG {
public static int countDigits( int a, int b)
{
double digits = Math.Log10(Math.Abs(a))
- Math.Log10(Math.Abs(b)) + 1;
return ( int )Math.Floor(digits);
}
static void Main()
{
int a = 100;
int b = 10;
Console.Write(countDigits(a, b));
}
}
|
PHP
<?php
function countDigits( $a , $b )
{
return floor (log10( abs ( $a )) -
log10( abs ( $b ))) + 1;
}
$a = 100;
$b = 10;
echo countDigits( $a , $b );
?>
|
Javascript
<script>
function countDigits(a, b)
{
return Math.floor((Math.log(Math.abs(a))/Math.log(10)) - (Math.log(Math.abs(b))/Math.log(10))) + 1;
}
var a = 100;
var b = 10;
document.write(countDigits(a, b));
</script>
|
Time Complexity: O(log10(a/ b))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...