Number of digits in 2 raised to power n - GeeksforGeeks

06 Apr, 2021
Let n be any power raised to base 2 i.e 2n. We are given the number n and our task is to find out the number of digits contained in the number 2n.Examples: 
Input : n = 5
Output : 2
Explanation : 2n = 32, which has only
2 digits.

Input : n = 10
Output : 4
Explanation : 2n = 1024, which has only
4 digits.
We can write 2n using logarithms as:  
2n = 10nlog102
Now suppose, x = nlog102, Therefore, 2n = 10xAlso, we all know that the number, 10n will have (n+1) digits. Therefore, 10x will have (x+1) digits.Or, we can say that 2n will have (x+1) digits as 2n = 10x.Therefore, number of digits in 2n = (nlog102) + 1Below is the implementation of above idea:  
C++
Java
Python3
C#
PHP
Javascript
// 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^nint countDigits(int n){    return (n * log10(2) + 1);} // Driver codeint main(){    int n = 5;    cout << countDigits(n) << endl;    return 0;}
// Java program to find number// of digits in 2^nimport 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 program to find# number of digits in 2^nimport math # Function to find number# of digits in 2^ndef countDigits(n):    return int(n * math.log10(2) + 1); # Driver coden = 5;print(countDigits(n)); # This code is contributed# by mits
// C# program to find number// of digits in 2^nusing 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 program to find// number of digits in 2^n // Function to find number// of digits in 2^nfunction countDigits($n){    return intval($n * log10(2) + 1);} // Driver code$n = 5;echo (countDigits($n)); // This code is contributed by// Manish Shaw(manishshaw1)?>
<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)
Niraj_Pandey
manishshaw1
Mithun Kumar
rishavmahato348
souravghosh0416
maths-log
number-digits
Mathematical
School Programming
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program to print prime numbers from 1 to N.
Modular multiplicative inverse
Fizz Buzz Implementation
Generate all permutation of a set in Python
How to check if a given point lies inside or outside a polygon?
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
C++ Classes and Objects