- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Number of Digits in a^b 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 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
Advertisements