Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Number of digits in 2 raised to power n in C++
The power of a number can be computed using the iterative multiplication or function that the language provides. It's a straightforward thing.
Here, we have to find the 2 raised to power n. And the number of digits in the result. Let's see some examples.
Input
5
Output
2
Input
10
Output
4
Algorithm
- Initialise the number n.
- Find the value of 2n.
- The ceil of log10(n) will give you number of digits in the number n.
- Find it and return it.
Implementation
Following is the implementation of the above algorithm in C++
#include <bits/stdc++.h>
using namespace std;
int getDigitsCount(int n) {
return ceil(log10(pow(2, n)));
}
int main() {
int n = 8;
cout << getDigitsCount(n) << endl;
return 0;
}
Output
If you run the above code, then you will get the following result.
3
Advertisements
