Convert a number from Base 2 (Binary) to Base 6The practical implementation of converting a binary number into a base 6 number is discussed in this article: Problem - The problem is to convert a given binary number into its equivalent number in base 6.For Example: Input 1: N = 1101101 = 10910 Output = 301 (3016 = 3 x 62 + 0 x 61 + 1 x 60 = 3 x 36 + 1 = 109) Input 2: N = 1110111 = 11910 Idea and approachThe simple idea to get the desired result is to first convert the given binary into its equivalent number in base 10 and then convert it into its equivalent number in base 6. Let us now understand the working/functions involved in finding the solution:
The pseudo-code of the required function is given below: Binary To Decimal (Int n) // Where n is the binary input
The pseudo-code of the required function is given below: Decimal To Base6 (Int dec) // Where dec is the decimal input Let's take an example to understand further: Let n = 1101, when we call the binary to decimal function, it will first create some variables and initialize them with some value. Here, dec = 0, and pv = 1 is created. Now, check the while-loop condition. Here, n = 1101, which is greater than zero so, we move to the first iteration. Iteration 1: Iteration 2: Iteration 3: Iteration 4: Here, the while-loop will terminate as n is now equal to zero. Now, it will call the decimalToBase6(dec) function and pass the dec = 13 to it. When the decimaltToBase6 function is called, it creates pv = 1 and output = 0. The dec = 13 is passed to this function and it is greater than zero so the while-loop condition is satisfied. Let's see each iteration one by one: Iteration 1: Iteration 2: here, dec is now equal to zero. So, in the next iteration, the condition of the while-loop will fail and it will print the output and exit from the program. Practical Implementation of the above problem1. Java Implementation:Sample Output: The Base6 equivalent of 1101101 = 301 The Base6 equivalent of 1110111 = 315 The Base6 equivalent of 1010111 = 223 The Base6 equivalent of 1101111 = 303 2. C++ ImplementationSample Output: The Base6 equivalent of 11011011 = 1003 The Base6 equivalent of 10110111 = 503 The Base6 equivalent of 10110111 = 503 The Base6 equivalent of 11011011 = 1003 3. C ImplementationSample Output: The Base6 equivalent of 101010 = 110 The Base6 equivalent of 111101 = 141 The Base6 equivalent of 101101 = 113 The Base6 equivalent of 111010 = 134 4. Python ImplementationSample Output: The base6 equivalent of 1010101 = 221 The base6 equivalent of 1111010 = 322 The base6 equivalent of 1011011 = 231 The base6 equivalent of 1110100 = 312 Next TopicCache Coherence |