0

I am not getting this code in my mind?

Can someone explain this code to me?.I am not getting the use of second loop here how's ot working https://code.sololearn.com/c2wWLpr8sSdV/?ref=app

26th Mar 2022, 3:20 AM
Muhammad Talha Atif
Muhammad Talha Atif - avatar
2 Answers
0
G'day proGAMBLER this is pretty cool. Your code makes an empty 10 digit array. It then expects a decimal input. It runs a loop (terminates when n is reduced to 0). Each loop uses modulo 2 to save either 1 or 0 to the array. Divides n by 2 Loop again if n isn't 0 Second part runs another loop, but this one counts down from i-1 down to 0 Prints the array digits in reverse order (giving a binary representation of the original digital input)
26th Mar 2022, 6:21 AM
HungryTradie
HungryTradie - avatar
0
So if user enters 10 10%2 is 0, save 0 to a[0], divide 10/2=5, advance i to become 1 5%2 is 1, save 1 to a[1], divide 5/2=2, advance i to become 2 2%2 is 0, save 0 to a[2], divide 2/2=1, advance i to become 3 1%2 is 1, save 1 to a[3], divide 1/2=0, advance i to become 4 Loop terminates because n isn't greater than 0 Array is now 0 1 0 1 Print reverse 1010 is binary for 10.
26th Mar 2022, 6:35 AM
HungryTradie
HungryTradie - avatar