Number of Digits in a^b

Given two positive integers a and b, task is to find the number of digits in a^b (a raised to the power b).

Example:

```Input: a = 2  b = 5
Output: no. of digits = 2
Explanation:
2^5 = 32
Hence, no. of digits = 2

Input: a = 2  b = 100
Output: no. of digits = 31
Explanation:
2^100 = 1.2676506e+30
Hence, no. of digits = 31
```

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

Approach:
The number of digits in a^b can be calculated using the formula:

`Number of Digits = 1 + b * (log10a)`

When a number is divided by 10, it is reduced by 1 digit.
Example:

`554 / 10 = 55, 55 / 10 = 5`

Notice, 554 initially has 3 digits but after division there are 2 digits 55 and after further division there is only 1 digit 5. So it can be concluded that to count number of digits, how many times a number is divided by 10 to reach 1 needs to be calculated.
log base 10 of a number is the number of times a number needs to be divided by 10 to reach 1 but as 1 itself is not included in log base 10, 1 is added to get the number of digits.
Note: Floor value of b * (log10a) is taken.

Below is the implementation to calculate the number of digits in a^b.

CPP

 `// CPP Program to calculate  ` `// no. of digits in a^b ` `#include ` `#include ` `using` `namespace` `std; ` ` `  `// function to calculate number ` `// of digits in a^b ` `int` `no_of_digit(``int` `a, ``int` `b) ` `{ ` `    ``return` `((``int``)(b * ``log10``(a)) + 1); ` `} ` `     `  `// driver program ` `int` `main() ` `{ ` `    ``int` `a = 2, b = 100; ` `    ``cout <<``"no. of digits = "``<< ` `                  ``no_of_digit(a, b); ` `} ` ` `  `// This code is contributed by Smitha `

Java

 `  `  `// Java Program to calculate  ` `// no. of digits in a^b ` `class` `GFG { ` `     `  `    ``// function to calculate number ` `    ``// of digits in a^b ` `    ``static` `int` `no_of_digit(``int` `a, ``int` `b) ` `    ``{ ` `        ``return` `((``int``)(b * Math.log10(a)) + ``1``); ` `    ``} ` `     `  `    ``// driver program ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `a = ``2``, b = ``100``; ` `        ``System.out.print(``"no. of digits = "` `+ ` `                          ``no_of_digit(a, b)); ` `    ``} ` `} `

Python3

 `# Python Program to calculate ` `# no. of digits in a^b ` `import` `math ` ` `  `# function to calculate number ` `# of digits in a^b ` `def` `no_of_digit(a, b): ` `    ``return` `((``int``)(b ``*` `math.log10(a)) ``+` `1``) ` ` `  `# Driver Program ` `a ``=` `2` `b ``=` `100` `print``(``"no of digits = "``, no_of_digit(a, b)) ` ` `  `# This code is contributed by Shrikant13 `

C#

 `// C# Program to calculate  ` `// no. of digits in a^b ` `using` `System; ` ` `  `class` `GFG { ` `     `  `    ``// function to calculate number ` `    ``// of digits in a^b ` `    ``static` `int` `no_of_digit(``int` `a, ``int` `b) ` `    ``{ ` `        ``return` `((``int``)(b * Math.Log10(a)) + 1); ` `    ``} ` `     `  `    ``// driver program ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `a = 2, b = 100; ` `        ``Console.Write(``"no. of digits = "` `+ ` `                        ``no_of_digit(a, b)); ` `    ``} ` `} ` ` `  `// This code is contributed by Smitha. `

PHP

 ` `

Output:

```no.of digits = 31
```

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 :
Practice Tags :

Be the First to upvote.

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