# Number of digits in the product of two numbers

Given two integers a and b. The problem is to find the number of digits in the product of these two integers.

Examples:

```Input : a = 12, b = 4
Output : 2
12 * 4 = 48 (2 digits)

Input : a = 33, b = -24
Output : 3
33 * -24 = -792 (3 digits)
```

## Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.

Naive Approach: Multiply the two numbers and then by using looping construct find the number of digits in the product. Take the absolute value of the product for finding the number of digits.

## C++

 `// C++ implementation to count number of digits ` `// in the product of two numbers ` `#include ` ` `  `using` `namespace` `std; ` ` `  `// function to count number of digits  ` `// in the product of two numbers ` `int` `countDigits(``int` `a, ``int` `b) ` `{ ` `    ``int` `count = 0;     ` `     `  `    ``// absolute value of the  ` `    ``// product of two numbers ` `    ``int` `p = ``abs``(a*b); ` `     `  `    ``// if product is 0 ` `    ``if` `(p == 0)     ` `        ``return` `1; ` `     `  `    ``// count number of digits in the product 'p'     ` `    ``while` `(p > 0)     ` `    ``{ ` `        ``count++; ` `        ``p = p / 10; ` `    ``} ` `     `  `    ``// required count of digits     ` `    ``return` `count; ` `} ` ` `  `// Driver program to test above ` `int` `main() ` `{ ` `    ``int` `a = 33; ` `    ``int` `b = -24; ` `    ``cout << ``"Number of digits = "` `         ``<< countDigits(a,b); ` `    ``return` `0; ` `} `

## Java

 `// Java implementation to count  ` `// number of digits in the product  ` `// of two numbers ` `import` `java.io.*; ` `import` `java.math.*; ` ` `  `class` `GFG { ` `     `  `    ``// function to count number of digits  ` `    ``// in the product of two numbers ` `    ``static` `int` `countDigits(``int` `a, ``int` `b) ` `    ``{ ` `        ``int` `count = ``0``;  ` `         `  `        ``// absolute value of the  ` `        ``// product of two numbers ` `        ``int` `p = Math.abs(a * b); ` `         `  `        ``// if product is 0 ` `        ``if` `(p == ``0``)  ` `            ``return` `1``; ` `         `  `        ``// count number of digits in ` `        ``// the product 'p'  ` `        ``while` `(p > ``0``)  ` `        ``{ ` `            ``count++; ` `            ``p = p / ``10``; ` `        ``} ` `         `  `        ``// required count of digits  ` `        ``return` `count; ` `    ``} ` `     `  `    ``// Driver program to test above ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` `        ``int` `a = ``33``; ` `        ``int` `b = -``24``; ` `        ``System.out.println(``"Number of digits = "` `                           ``+ countDigits(a, b)); ` `    ``} ` `} ` ` `  `/*This code is contributed by Nikita Tiwari.*/`

## Python3

 `# Python 3 implementation to count  ` `# number of digits in the product  ` `# of two numbers ` ` `  `# function to count number of digits  ` `# in the product of two numbers ` `def` `countDigits(a, b) : ` `    ``count ``=` `0` `     `  `    ``# absolute value of the  ` `    ``# product of two numbers ` `    ``p ``=` `abs``(a ``*` `b) ` `     `  `    ``# if product is 0 ` `    ``if` `(p ``=``=` `0``) : ` `        ``return` `1` `     `  `    ``# count number of digits  ` `    ``# in the product 'p'  ` `    ``while` `(p > ``0``) : ` `        ``count ``=` `count ``+` `1` `        ``p ``=` `p ``/``/` `10` `     `  `     `  `    ``# required count of digits  ` `    ``return` `count ` ` `  ` `  `# Driver program to test above ` `a ``=` `33` `b ``=` `-``24` `print``(``"Number of digits = "``, ` `       ``countDigits(a,b)) ` ` `  `# This code is contributed by Nikita Tiwari. `

## C#

 `// C# implementation to count ` `// number of digits in the product ` `// of two numbers ` `using` `System; ` ` `  `class` `GFG { ` `     `  `    ``// function to count number of digits ` `    ``// in the product of two numbers ` `    ``static` `int` `countDigits(``int` `a, ``int` `b) ` `    ``{ ` `        ``int` `count = 0; ` ` `  `        ``// absolute value of the ` `        ``// product of two numbers ` `        ``int` `p = Math.Abs(a * b); ` ` `  `        ``// if product is 0 ` `        ``if` `(p == 0) ` `            ``return` `1; ` ` `  `        ``// count number of digits in ` `        ``// the product 'p' ` `        ``while` `(p > 0) { ` `            ``count++; ` `            ``p = p / 10; ` `        ``} ` ` `  `        ``// required count of digits ` `        ``return` `count; ` `    ``} ` ` `  `    ``// Driver program to test above ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `a = 33; ` `        ``int` `b = -24; ` `        ``Console.WriteLine(``"Number of digits = "` `+ ` `                              ``countDigits(a, b)); ` `    ``} ` `} ` ` `  `// This code is contributed by Sam007 `

## PHP

 ` 0)  ` `    ``{ ` `        ``\$count``++; ` `        ``\$p` `= (int)(``\$p` `/ 10); ` `    ``} ` `     `  `    ``// required count of digits  ` `    ``return` `\$count``; ` `} ` ` `  `// Driver Code ` `\$a` `= 33; ` `\$b` `= -24; ` `echo` `"Number of digits = "` `.  ` `        ``countDigits(``\$a``, ``\$b``); ` ` `  `// This code is contributed by mits ` `?> `

Output:

```Number of digits = 3
```

Efficient Approach: To count the number of digits in the product of two numbers we can use the formula given below:

```                count = 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.

## C++

 `// C++ implementation to count number of digits ` `// in the product of two numbers ` `#include ` ` `  `using` `namespace` `std; ` ` `  `// function to count number of digits  ` `// in the product of two numbers ` `int` `countDigits(``int` `a, ``int` `b) ` `{ ` `    ``// if either of the number is 0, then ` `    ``// product will be 0 ` `    ``if` `(a == 0 || b == 0) ` `        ``return` `1; ` `         `  `    ``// required count of digits             ` `    ``return` `floor``(``log10``(``abs``(a)) + ``log10``(``abs``(b))) + 1;     ` `} ` ` `  `// Driver program to test above ` `int` `main() ` `{ ` `    ``int` `a = 33; ` `    ``int` `b = -24; ` `    ``cout << countDigits(a,b); ` `    ``return` `0; ` `} `

## Java

 `// JAVA Code for Number of digits  ` `// in the product of two numbers ` `class` `GFG { ` `     `  `    ``// function to count number of digits  ` `    ``// in the product of two numbers ` `    ``public` `static` `int` `countDigits(``int` `a, ``int` `b) ` `    ``{ ` `        ``// if either of the number is 0, then ` `        ``// product will be 0 ` `        ``if` `(a == ``0` `|| b == ``0``) ` `            ``return` `1``; ` `              `  `        ``// required count of digits             ` `        ``return` `(``int``)Math.floor(Math.log10(Math.abs(a)) +  ` `                            ``Math.log10(Math.abs(b))) + ``1``;     ` `    ``} ` `     `  `    ``/* Driver program to test above function */` `    ``public` `static` `void` `main(String[] args)  ` `    ``{ ` `        ``int` `a = ``33``; ` `        ``int` `b = -``24``; ` `        ``System.out.print(countDigits(a,b)); ` `    ``} ` `} ` `// This code is contributed by Arnav Kr. Mandal. `

## Python3

 `# Python 3 implementation to count ` `# number of digits in the product ` `# of two numbers ` `import` `math  ` ` `  `# function to count number of digits  ` `# in the product of two numbers ` `def` `countDigits(a, b) : ` `     `  `    ``# if either of the number is 0,  ` `    ``# then product will be 0 ` `    ``if` `(a ``=``=` `0` `or` `b ``=``=` `0``) : ` `        ``return` `1` `         `  `    ``# required count of digits          ` `    ``return` `math.floor(math.log10(``abs``(a)) ``+`  `                   ``math.log10(``abs``(b))) ``+` `1` ` `  ` `  `# Driver program to test above ` `a ``=` `33` `b ``=` `-``24` `print``(countDigits(a, b)) ` ` `  `# This code is contributed by Nikita Tiwari. `

## C#

 `// C# Code for Number of digits  ` `// in the product of two numbers ` `using` `System; ` ` `  `class` `GFG { ` `     `  `    ``// function to count number of ` `    ``// digits in the product of two  ` `    ``// numbers ` `    ``public` `static` `int` `countDigits(``int` `a, ` `                                  ``int` `b) ` `    ``{ ` `        ``// if either of the number is 0, ` `        ``// then product will be 0 ` `        ``if` `(a == 0 || b == 0) ` `            ``return` `1; ` `             `  `        ``// required count of digits          ` `        ``return` `(``int``)Math.Floor( ` `                 ``Math.Log10(Math.Abs(a)) ` `          ``+ Math.Log10(Math.Abs(b))) + 1;  ` `    ``} ` `     `  `    ``// Driver code ` `    ``static` `void` `Main() ` `    ``{ ` `        ``int` `a = 33; ` `        ``int` `b = -24; ` `        ``Console.Write(countDigits(a, b)); ` `         `  `    ``} ` `} ` ` `  `// This code is contributed by Sam007 `

## PHP

 ` `

Output:

```3
```

This article is contributed by Ayush Jauhari. 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.
{"ft_lang_label":"__label__en","ft_lang_prob":0.6379745,"math_prob":0.98620766,"size":8229,"snap":"2019-51-2020-05","text_gpt3_token_len":2415,"char_repetition_ratio":0.25908816,"word_repetition_ratio":0.5038393,"special_character_ratio":0.32786486,"punctuation_ratio":0.102661595,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99991703,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-12-14T14:37:32Z\",\"WARC-Record-ID\":\"<urn:uuid:273be307-62a7-48f8-b2a9-0a8d2b7a624d>\",\"Content-Length\":\"204473\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:f4f76f48-364c-41e1-82fb-f4f85ac2c660>\",\"WARC-Concurrent-To\":\"<urn:uuid:6d8daa78-7d35-45f5-866d-2f34cd172b18>\",\"WARC-IP-Address\":\"23.221.72.17\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/number-digits-product-two-numbers/\",\"WARC-Payload-Digest\":\"sha1:ED7VHFZSJ3KYXIYUE54FRYJKDEW3Q7TD\",\"WARC-Block-Digest\":\"sha1:J4SV7NY2CXELWRTTOG4V3PU4NXP5UNPA\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-51/CC-MAIN-2019-51_segments_1575541157498.50_warc_CC-MAIN-20191214122253-20191214150253-00354.warc.gz\"}"}