# Number of digits before the decimal point in the division of two numbers

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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return the number of digits ` `// before the decimal in a / b ` `int` `countDigits(``int` `a, ``int` `b) ` `{ ` `    ``int` `count = 0; ` ` `  `    ``// Absolute value of a / b ` `    ``int` `p = ``abs``(a / b); ` ` `  `    ``// If result is 0 ` `    ``if` `(p == 0) ` `        ``return` `1; ` ` `  `    ``// Count number of digits in the result ` `    ``while` `(p > 0) { ` `        ``count++; ` `        ``p = p / 10; ` `    ``} ` ` `  `    ``// Return the required count of digits ` `    ``return` `count; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `a = 100; ` `    ``int` `b = 10; ` `    ``cout << countDigits(a, b); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach ` `class` `GFG { ` ` `  `    ``// Function to return the number of digits ` `    ``// before the decimal in a / b ` `    ``static` `int` `countDigits(``int` `a, ``int` `b) ` `    ``{ ` `        ``int` `count = ``0``; ` ` `  `        ``// Absolute value of a / b ` `        ``int` `p = Math.abs(a / b); ` ` `  `        ``// If result is 0 ` `        ``if` `(p == ``0``) ` `            ``return` `1``; ` ` `  `        ``// Count number of digits in the result ` `        ``while` `(p > ``0``) { ` `            ``count++; ` `            ``p = p / ``10``; ` `        ``} ` ` `  `        ``// Return the required count of digits ` `        ``return` `count; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` `        ``int` `a = ``100``; ` `        ``int` `b = ``10``; ` `        ``System.out.print(countDigits(a, b)); ` `    ``} ` `} `

## Python

 `# Python 3 implementation of the approach ` ` `  `# Function to return the number of digits  ` `# before the decimal in a / b ` `def` `countDigits(a, b): ` `    ``count ``=` `0` `     `  `    ``# Absolute value of a / b ` `    ``p ``=` `abs``(a ``/``/` `b) ` `     `  `    ``# If result is 0 ` `    ``if` `(p ``=``=` `0``): ` `        ``return` `1` `     `  `    ``# Count number of digits in the result ` `    ``while` `(p > ``0``): ` `        ``count ``=` `count ``+` `1` `        ``p ``=` `p ``/``/` `10` `     `  `     `  `    ``# Return the required count of digits ` `    ``return` `count  ` ` `  `# Driver code ` `a ``=` `100` `b ``=` `10` `print``(countDigits(a, b)) `

## C#

 `// C# implementation of the approach ` `using` `System; ` `class` `GFG { ` ` `  `    ``// Function to return the number of digits ` `    ``// before the decimal in a / b ` `    ``static` `int` `countDigits(``int` `a, ``int` `b) ` `    ``{ ` `        ``int` `count = 0; ` ` `  `        ``// Absolute value of a / b ` `        ``int` `p = Math.Abs(a / b); ` ` `  `        ``// If result is 0 ` `        ``if` `(p == 0) ` `            ``return` `1; ` ` `  `        ``// Count number of digits in the result ` `        ``while` `(p > 0) { ` `            ``count++; ` `            ``p = p / 10; ` `        ``} ` ` `  `        ``// Return the required count of digits ` `        ``return` `count; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `a = 100; ` `        ``int` `b = 10; ` `        ``Console.Write(countDigits(a, b)); ` `    ``} ` `} `

## PHP

 ` 0) {  ` `        ``\$count``++;  ` `        ``\$p` `= (int)(``\$p` `/ 10);  ` `    ``}  ` `     `  `    ``// Return the required count of digits ` `    ``return` `\$count``;  ` `}  ` ` `  `// Driver code  ` `\$a` `= 100;  ` `\$b` `= 10;  ` `echo` `countDigits(``\$a``, ``\$b``);  ` `?> `

Output:

```2
```

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++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return the number of digits ` `// before the decimal in a / b ` `int` `countDigits(``int` `a, ``int` `b) ` `{ ` `    ``// Return the required count of digits ` `    ``return` `floor``(``log10``(``abs``(a)) - ``log10``(``abs``(b))) + 1; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `a = 100; ` `    ``int` `b = 10; ` `    ``cout << countDigits(a, b); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach ` `class` `GFG { ` ` `  `    ``// Function to return the number of digits ` `    ``// before the decimal in a / b ` `    ``public` `static` `int` `countDigits(``int` `a, ``int` `b) ` `    ``{ ` `        ``double` `digits = Math.log10(Math.abs(a)) ` `                        ``- Math.log10(Math.abs(b)) + ``1``; ` ` `  `        ``// Return the required count of digits ` `        ``return` `(``int``)Math.floor(digits); ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `a = ``100``; ` `        ``int` `b = ``10``; ` `        ``System.out.print(countDigits(a, b)); ` `    ``} ` `} `

## Python

 `# Python3 implementation of the approach ` `import` `math  ` ` `  `# Function to return the number of digits  ` `# before the decimal in a / b ` `def` `countDigits(a, b): ` `     `  `    ``# Return the required count of digits      ` `    ``return` `math.floor(math.log10(``abs``(a)) ``-` `                ``math.log10(``abs``(b))) ``+` `1` ` `  ` `  `# Driver code ` `a ``=` `100` `b ``=` `10` `print``(countDigits(a, b)) `

## C#

 `// C# implementation of the approach ` `using` `System; ` `class` `GFG { ` ` `  `    ``// Function to return the number of digits ` `    ``// before the decimal in a / b ` `    ``public` `static` `int` `countDigits(``int` `a, ``int` `b) ` `    ``{ ` `        ``double` `digits = Math.Log10(Math.Abs(a)) ` `                        ``- Math.Log10(Math.Abs(b)) + 1; ` ` `  `        ``// Return the required count of digits ` `        ``return` `(``int``)Math.Floor(digits); ` `    ``} ` ` `  `    ``// Driver code ` `    ``static` `void` `Main() ` `    ``{ ` `        ``int` `a = 100; ` `        ``int` `b = 10; ` `        ``Console.Write(countDigits(a, b)); ` `    ``} ` `} `

## PHP

 `  `

Output:

```2
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.