- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 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
- Related Articles
- Find last five digits of a given five digit number raised to power five in C++
- Find value of y mod (2 raised to power x) in C++
- Sum of the digits of a number N written in all bases from 2 to N/2 in C++
- C/C++ Program to Find sum of Series with n-th term as n power of 2 - (n-1) power of 2
- Minimum removals in a number to be divisible by 10 power raised to K in C++
- Count total number of digits from 1 to N in C++
- Program to find last two digits of 2^n in C++
- Power of a prime number r in n! in C++
- Count digits in given number N which divide N in C++
- K-th digit in ‘a’ raised to power ‘b’ in C++
- n-th number with digits in {0, 1, 2, 3, 4, 5} in C++
- Finding the power of prime number p in n! in C++
- To return value of a number raised to the power of another number, we should use ^ operator in MySQL?
- Finding n-th number made of prime digits (2, 3, 5 and 7) only in C++
- Why is any number raised to the power 0 always 1?

Advertisements