Number of Digits in a^b in C++

C++Server Side ProgrammingProgramming

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 a raised to power b. And the number of digits in the result. Let's see some examples.

Input

a = 5
b = 2

Output

2


Input

a = 7
b = 6

Output

6


Algorithm

• Initialise the number a and b.
• Find the value of ab.
• 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 a, int b) {
return ceil(log10(pow(a, b)));
}
int main() {
int a = 8;
int b = 3;
cout << getDigitsCount(a, b) << endl;
return 0;
}

Output

If you run the above code, then you will get the following result.

3
Published on 03-Jul-2021 04:59:21