# Find i’th Index character in a binary string obtained after n iterations

Given a decimal number m, convert it into a binary string and apply n iterations, in each iteration 0 becomes “01” and 1 becomes “10”. Find ith(based indexing) index character in the string after nth iteration.

Examples :

```Input : m = 5, n = 2, i = 3
Output : 1

Input :m = 3, n = 3, i = 6
Output : 1
```

# Recommended: Please solve it on “PRACTICE“first, before moving on to the solution.

1. Change a decimal number into binary and store in string s.
2. Run loop n times in each iteration. Run another loop of string
length s to convert 0 to “01” and 1 to “10” and store in another string s1.
After completion of each iteration, assign string s1 to s.
3. At last Return the value of ith index in string s.

## C++

 `// C++ Program to find ith character in  ` `// a binary string. ` `#include ` `using` `namespace` `std; ` ` `  `// Function to store binary Representation ` `void` `binary_conversion(string &s, ``int` `m) { ` `  ``while` `(m) { ` `    ``int` `tmp = m % 2; ` `    ``s += tmp + ``'0'``; ` `    ``m = m / 2; ` `  ``} ` `  ``reverse(s.begin(), s.end()); ` `} ` ` `  `// Function to find ith character ` `int` `find_character(``int` `n, ``int` `m, ``int` `i) { ` ` `  `  ``string s; ` ` `  `  ``// Function to change decimal to binary ` `  ``binary_conversion(s, m); ` ` `  `  ``string s1 = ``""``; ` `  ``for` `(``int` `x = 0; x < n; x++) { ` `    ``for` `(``int` `y = 0; y < s.length(); y++) { ` `      ``if` `(s[y] == ``'1'``)  ` `        ``s1 += ``"10"``; ` `      ``else`  `        ``s1 += ``"01"``;       ` `    ``} ` ` `  `    ``// Assign s1 string in s string ` `    ``s = s1; ` `    ``s1 = ``""``; ` `  ``} ` `  ``return` `s[i] - ``'0'``; ` `} ` ` `  `// Driver Function ` `int` `main() { ` `  ``int` `m = 5, n = 2, i = 8; ` `  ``cout << find_character(n, m, i); ` `  ``return` `0; ` `} `

## Java

 `// Java Program to find ith  ` `// character in a binary String. ` `import` `java.io.*; ` `import` `java.util.Arrays; ` ` `  `class` `GFG ` `{ ` `static` `String s = ``""``; ` `static` `String ReverseString(String s) ` `{ ` `    ``char``[] arr = s.toCharArray(); ` `    ``for``(``int` `i = ``0``; ` `            ``i < arr.length / ``2``; i++) ` `    ``{ ` `        ``char` `temp = arr[i];  ` `        ``arr[i] = arr[arr.length - i -``1``];  ` `        ``arr[arr.length - i - ``1``] = temp;  ` `    ``} ` `    ``return` `new` `String(arr); ` `} ` ` `  `// Function to store ` `// binary Representation ` `static` `void` `binary_conversion(``int` `m)  ` `{ ` `    ``while` `(m != ``0``)  ` `    ``{ ` `        ``int` `tmp = m % ``2``; ` `        ``s += Integer.toString(tmp); ` `        ``m = (``int``)(m / ``2``); ` `    ``} ` `     `  `    ``s = ReverseString(s); ` `} ` ` `  `// Function to find  ` `// ith character ` `static` `int` `find_character(``int` `n,  ` `                          ``int` `m,  ` `                          ``int` `i)  ` `{      ` `    ``// Function to change ` `    ``// decimal to binary ` `    ``binary_conversion(m); ` ` `  `    ``String s1 = ``""``; ` `    ``for` `(``int` `x = ``0``; x < n; x++)  ` `    ``{ ` `        ``for` `(``int` `y = ``0``;  ` `                 ``y < s.length(); y++)  ` `        ``{ ` `            ``if` `(s.charAt(y) == ``'1'``)  ` `            ``s1 += ``"10"``; ` `            ``else` `            ``s1 += ``"01"``;      ` `        ``} ` `         `  `        ``// Assign s1 String  ` `        ``// in s String ` `        ``s = s1; ` `        ``s1 = ``""``; ` `    ``} ` `     `  `    ``return` `s.charAt(i) - ``'0'``; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main(String args[]) ` `{ ` `    ``int` `m = ``5``, n = ``2``, i = ``8``; ` `    ``System.out.print( ` `               ``find_character(n, m, i)); ` `} ` `} ` ` `  `// This code is contributed by  ` `// Manish Shaw(manishshaw1) `

## Python3

 `# Python3 Program to find ith character in  ` `# a binary string. ` ` `  `# Function to store binary Representation ` `def` `binary_conversion(s, m): ` `    ``while``(m): ` `        ``temp ``=` `m ``%` `2` `        ``s ``+``=` `str``(temp) ` `        ``m ``=` `m ``/``/` `2` `     `  `    ``return` `s[::``-``1``] ` ` `  `# Function to find ith character ` `def` `find_character(n, m, i): ` `    ``s ``=` `"" ` ` `  ` `  `# Function to change decimal to binary ` `    ``s ``=` `binary_conversion(s, m) ` `    ``s1 ``=` `"" ` ` `  `    ``for` `x ``in` `range``(n): ` `        ``for` `j ``in` `range``(``len``(s)): ` `            ``if` `s[j] ``=``=` `"1"``: ` `                ``s1 ``+``=` `"10"` `            ``else``: ` `                ``s1 ``+``=` `"01"` ` `  `    ``# Assign s1 string in s string      ` `        ``s ``=` `s1 ` `        ``s1 ``=` `"" ` `    ``e ``=` `ord``(s[i]) ` `    ``r ``=` `ord``(``'0'``) ` ` `  `    ``return` `e``-``r ` ` `  `# Driver code ` `m, n, i ``=` `5``, ``2``, ``8` ` `  `print``(find_character(n,m,i)) ` ` `  `# This code is contributed by mohit kumar 29 `

## C#

 `// C# Program to find ith  ` `// character in a binary string. ` `using` `System; ` ` `  `class` `GFG ` `{ ` `    ``static` `string` `ReverseString(``string` `s) ` `    ``{ ` `        ``char``[] arr = s.ToCharArray(); ` `        ``Array.Reverse(arr); ` `        ``return` `new` `string``(arr); ` `    ``} ` `     `  `    ``// Function to store ` `    ``// binary Representation ` `    ``static` `void` `binary_conversion(``ref` `string` `s,  ` `                                  ``int` `m)  ` `    ``{ ` `        ``while` `(m != 0)  ` `        ``{ ` `            ``int` `tmp = m % 2; ` `            ``s += tmp.ToString(); ` `            ``m = (``int``)(m / 2); ` `        ``} ` `         `  `        ``s = ReverseString(s); ` `    ``} ` `     `  `    ``// Function to find  ` `    ``// ith character ` `    ``static` `int` `find_character(``int` `n,  ` `                              ``int` `m, ``int` `i)  ` `    ``{      ` `        ``string` `s = ``""``; ` ` `  `        ``// Function to change ` `        ``// decimal to binary ` `        ``binary_conversion(``ref` `s, m); ` ` `  `        ``string` `s1 = ``""``; ` `        ``for` `(``int` `x = 0; x < n; x++)  ` `        ``{ ` `            ``for` `(``int` `y = 0; y < s.Length; y++)  ` `            ``{ ` `                ``if` `(s[y] == ``'1'``)  ` `                ``s1 += ``"10"``; ` `                ``else` `                ``s1 += ``"01"``;      ` `            ``} ` `             `  `            ``// Assign s1 string  ` `            ``// in s string ` `            ``s = s1; ` `            ``s1 = ``""``; ` `        ``} ` `         `  `        ``return` `s[i] - ``'0'``; ` `    ``} ` ` `  `    ``// Driver Code ` `    ``static` `void` `Main() ` `    ``{ ` `        ``int` `m = 5, n = 2, i = 8; ` `        ``Console.Write(find_character(n, m, i)); ` `    ``} ` `} ` ` `  `// This code is contributed by  ` `// Manish Shaw(manishshaw1) `

Output:

```1
```

Refer Set-2 for an optimized solution.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

2

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
{"ft_lang_label":"__label__en","ft_lang_prob":0.6500541,"math_prob":0.95745575,"size":5829,"snap":"2020-10-2020-16","text_gpt3_token_len":1808,"char_repetition_ratio":0.15948498,"word_repetition_ratio":0.27885425,"special_character_ratio":0.34997427,"punctuation_ratio":0.15224913,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9994991,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-04-03T21:04:31Z\",\"WARC-Record-ID\":\"<urn:uuid:89052809-65b8-4d97-937b-3b03e651f0d8>\",\"Content-Length\":\"170510\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:066a0759-eb83-495f-8110-3b852ddab333>\",\"WARC-Concurrent-To\":\"<urn:uuid:70b33776-0246-4964-a2d6-c29b8c99a6c6>\",\"WARC-IP-Address\":\"23.199.63.170\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/find-ith-index-character-in-a-binary-string-obtained-after-n-iterations/\",\"WARC-Payload-Digest\":\"sha1:URZ567FAQPJQHMLMUTQHUG4N77PRTA3G\",\"WARC-Block-Digest\":\"sha1:P5DK5DU3DOQD2AEGHB3QKF6NVCBB2TOQ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-16/CC-MAIN-2020-16_segments_1585370518622.65_warc_CC-MAIN-20200403190006-20200403220006-00156.warc.gz\"}"}