void WriteInBinary(int n) { if(n < 2) { printf("%i", n); } else { WriteInBinary(n/2); printf("%i", n%2); } }
Notice that, if n < 2, then n % 2 = n. That allows us to shorten the above definition as follows.
//======================================================= // WriteInBinary //======================================================= // WriteInBinary(n) writes out the binary representation // of n. //======================================================= void WriteInBinary(int n) { if(n ≥ 2) { WriteInBinary(n/2); } printf("%d", n%2); }