listingswhich can insert directly form a source code file and generate necessary formatting in Latex document. Just use the package in your Latex document file :
And then use the following command where you want to place code :
For more details see the link :
Also take a look in the following link for writing pseudo-code and algorithms in Latex.