Let n be any power raised to base 2 i.e 2^{n}. We are given the number n and our task is to find out the number of digits contained in the number 2^{n}.**Examples:**

Input :n = 5Output :2Explanation :2^{n}= 32, which has only 2 digits.Input :n = 10Output :4Explanation :2^{n}= 1024, which has only 4 digits.

We can write 2^{n} using logarithms as:

2^{n} = 10^{nlog}_{10}^{2}

Now suppose, x = nlog_{10}2,

Therefore, 2^{n} = 10^{x}

Also, we all know that the number, 10^{n} will have (n+1) digits. Therefore, 10^{x} will have (x+1) digits.

Or, we can say that 2^{n} will have (x+1) digits as 2^{n} = 10^{x}.

Therefore, number of digits in 2^{n} = (nlog_{10}2) + 1

Below is the implementation of above idea:

## C++

`// CPP program to find number of digits` `// in 2^n` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to find number of digits` `// in 2^n` `int` `countDigits(` `int` `n)` `{` ` ` `return` `(n * ` `log10` `(2) + 1);` `}` `// Driver code` `int` `main()` `{` ` ` `int` `n = 5;` ` ` `cout << countDigits(n) << endl;` ` ` `return` `0;` `}` |

## Java

`// Java program to find number` `// of digits in 2^n` `import` `java.util.*;` `class` `Gfg` `{` ` ` `// Function to find number of digits` ` ` `// in 2^n` ` ` `static` `int` `countDigits(` `int` `n)` ` ` `{` ` ` `return` `(` `int` `)(n * Math.log10(` `2` `) + ` `1` `);` ` ` `}` ` ` ` ` `// Driver Code` ` ` `public` `static` `void` `main(String args[])` ` ` `{` ` ` `int` `n = ` `5` `;` ` ` `System.out.println(countDigits(n));` ` ` `}` `}` `// This code is contributed by Niraj_Pandey.` |

## Python3

`# Python3 program to find` `# number of digits in 2^n` `import` `math` `# Function to find number` `# of digits in 2^n` `def` `countDigits(n):` ` ` `return` `int` `(n ` `*` `math.log10(` `2` `) ` `+` `1` `);` `# Driver code` `n ` `=` `5` `;` `print` `(countDigits(n));` `# This code is contributed` `# by mits` |

## C#

`// C# program to find number` `// of digits in 2^n` `using` `System;` `class` `GFG` `{` ` ` `// Function to find` ` ` `// number of digits in 2^n` ` ` `static` `int` `countDigits(` `int` `n)` ` ` `{` ` ` `return` `(` `int` `)(n * Math.Log10(2) + 1);` ` ` `}` ` ` ` ` `// Driver code` ` ` `static` `void` `Main()` ` ` `{` ` ` `int` `n = 5;` ` ` `Console.Write(countDigits(n));` ` ` `}` `}` `// This code is contributed by` `// Manish Shaw(manishshaw1)` |

## PHP

`<?php` `// PHP program to find` `// number of digits in 2^n` `// Function to find number` `// of digits in 2^n` `function` `countDigits(` `$n` `)` `{` ` ` `return` `intval` `(` `$n` `* log10(2) + 1);` `}` `// Driver code` `$n` `= 5;` `echo` `(countDigits(` `$n` `));` `// This code is contributed by` `// Manish Shaw(manishshaw1)` `?>` |

## Javascript

`<script>` `// JavaScript program to find number` `// of digits in 2^n` ` ` `// Function to find number of digits` ` ` `// in 2^n` ` ` `function` `countDigits(n)` ` ` `{` ` ` `return` `(n * Math.log10(2) + 1);` ` ` `}` `// Driver code` ` ` `let n = 5;` ` ` `document.write(Math.floor(countDigits(n)));` ` ` ` ` `// This code is contributed by souravghosh0416.` `</script>` |

**Output:**

2

**Time Complexity: **O(n)

**Auxiliary Space: **O(1)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the **Essential Maths for CP Course** at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**