Doubt in logic of the code

https://ide.codingblocks.com/s/331949 can you please check what is wrong with my code and please explain if there exists another approach

Modified code

i did not understand the logic behind append function can you please explain