Find the count of numbers that can be formed using digits 3 and 4 only and having length at max N in C++

C++Server Side ProgrammingProgramming

Given a number N. We have to find the count of such numbers that can be formed using digit 3 and 4. So if N = 6, then the numbers will be 3, 4, 33, 34, 43, 44.

We can solve this problem if we look closely, for single digit number it has 2 numbers 3 and 4, for digit 2, it has 4 numbers 33, 34, 43, 44. So for m digit numbers, it will have 2m values.

Example

 Live Demo

#include<iostream>
#include<cmath>
using namespace std;
long long countNumbers(int n) {
   return (long long)(pow(2, n + 1)) - 2;
}
int main() {
   int n = 3;
   cout << "Number of values: " << countNumbers(n);
}

Output

Number of values: 14
raja
Published on 19-Dec-2019 12:27:12
Advertisements