Decimal to Binary Method | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Decimal to Binary Method

I am writing a code to change decimal number to binary using recursion, where I am confused when calling the method inside a method. Here is my code: 1. public static void binaryEncoding(int n) { 2. int remainder; 3. if (n > 0) { 4. remainder = n % 2; 5. binaryEncoding(n / 2); 6. System.out.print(remainder); 7. } 8. } Let's say the number given is 5, line4: remainder = 1 from 5 % 2 line5: binaryEncoding(5 / 2), which is 4 however, where is the step 4 % 2 performed? When there is no loop in this method?

6th Dec 2018, 10:21 AM
Poo
5 Answers
+ 8
As you say n=5 So 5>0 //remainder will return the remaining value after divide by 2 5%2=1 //remainder 5/2=2 //after divide value is 2 So n=2 And remainder=1 is print Then 2%2=0 And 2/2=1 so n=1 remainder=0 is print Then 1%2 =1 1/2=0 So n=0 And remainder = 1 print so all remainder will be 101 which is equalivent to 5 in binary And remainder =0 so loop halt
6th Dec 2018, 10:32 AM
GAWEN STEASY
GAWEN STEASY - avatar
+ 7
Yes because it is an conditional statement it will execute till the condition is true
6th Dec 2018, 10:41 AM
GAWEN STEASY
GAWEN STEASY - avatar
+ 1
however, it is a 'if' statement, it will perform same as loop?
6th Dec 2018, 10:38 AM
Poo
0
Ok, it is not the same. It looks similar, true, but in fact it is more like two loops which looping first one until condition met (base case) than other one in opposite direction. In your example it is going to perform this way: (this is copy of GAWEN STEASY explanation modified) "As you say n=5 /* not a very happy choice of number, because it is symetrical 101 in binary representation, so I will use 4 instead which is 100 */ So 5>0 /* 4>0 true */ //remainder will return the remaining value after divide by 2 5%2=1 //remainder /* 4%2=0 */ 5/2=2 //after divide value is 2 /* 4/2=2 */ So n=2 /* also the same */ And remainder=1 is print /* this is crucal step for understanding difference between recursion and loop, nothing is printed but method is called again with some change and that change is n=2 instead of 4. Remainder, which is 0 in this case, still waits for something (printing in this case) after called method finish. Remainder value let's call A(0) will hold on stack. */ 2%2=0 /remainder B(0) on stack
8th Dec 2018, 5:58 AM
vlada
0
/*...contined... */ 2/2=1 calling method again with new value 1 1%2=1 /* remainder C(1) waits on stack */ 1/2=0 calling method again with value n=0 Base condition (n>0) not met, so method ends. Now we are going backward. Let's do what is remained. After every method call, there is just one print statement. 1. Print C ->1 - done, nothing to do more, method (call for n=1) ends. 2. Print B -> 0 - done, method (call for n=2) ends 3. Print A -> 0 - done, method (call for n=4) ends. If this looks confusing, try to change System.out.print(remainder) with System.out.println("remainder: " + remainder + " n: " + n);
8th Dec 2018, 6:20 AM
vlada