<code>
and <pre>
around the code block is not always enough. If you are struggling to format your source code, try FormatMySourceCode.
It has done an excellent job with the C/C++ snippets I've converted.
Since it seems to try to maintain the original formatting, it should
work fine with other languages as well.