# Number of digits in the nth number made of given four digits in C++

C++Server Side ProgrammingProgramming

In this tutorial, we are going to write a program that finds the number of digits in the nth number made of given four digits 1, 2, 3, and 4.

The series with the above four digits is as follows

1, 2, 3, 4, 11, 12, 13, 14, 21, 22, 23, 24...

We need to find the number of digits of the nth number from the above series. If you carefully observe the pattern, you will find the following points.

• There are 4 numbers with digits 1.

• There are 16 numbers with digits 2.

• The pattern continues as the powers of 4.

Create a loop which increased the iterative variable by the powers of 4. And count the number of iterations until the sum of all seen numbers is not greater than the given number n.

## Example

Let's see the code.

Live Demo

#include <bits/stdc++.h>
using namespace std;
int getDigitsCount(int n) {
int i, result = 1, sum = 0;
for (i = 4; ; i *= 4) {
sum += i;
if (sum >= n) {
break;
}
result += 1;
}
return result;
}
int main() {
int n = 57;
cout << getDigitsCount(n) << endl;
return 0;
}

## Output

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

3

## Conclusion

If you have any queries in the tutorial, mention them in the comment section.
{"ft_lang_label":"__label__en","ft_lang_prob":0.81029314,"math_prob":0.99300486,"size":1118,"snap":"2021-31-2021-39","text_gpt3_token_len":317,"char_repetition_ratio":0.1319569,"word_repetition_ratio":0.0,"special_character_ratio":0.31484795,"punctuation_ratio":0.17898832,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9990897,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-09-28T11:43:46Z\",\"WARC-Record-ID\":\"<urn:uuid:c5c862b5-ffa0-48f8-8c7f-2a66a64501ac>\",\"Content-Length\":\"31946\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:09f79b58-4c79-470a-b933-54833553d305>\",\"WARC-Concurrent-To\":\"<urn:uuid:1b739c7d-bd27-4cea-b0f6-371a28ab380a>\",\"WARC-IP-Address\":\"72.21.91.42\",\"WARC-Target-URI\":\"https://www.tutorialspoint.com/number-of-digits-in-the-nth-number-made-of-given-four-digits-in-cplusplus\",\"WARC-Payload-Digest\":\"sha1:BELFXSA6QPCXZ5XKKVQVHT4LFYJPU53P\",\"WARC-Block-Digest\":\"sha1:AGRLXCVIUKFAJMGGZBAN4U6WLIT5SPS4\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-39/CC-MAIN-2021-39_segments_1631780060677.55_warc_CC-MAIN-20210928092646-20210928122646-00605.warc.gz\"}"}