void WriteInBinary(const int n)
{
if(n <= 1)
{
printf("%i", n);
}
else
{
WriteInBinary(n/2);
printf("%i", n%2);
}
}
Notice that, if n ≤ 1, 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(const int n)
{
if(n > 1)
{
WriteInBinary(n/2);
}
printf("%d", n%2);
}