Agreed with Ihamil64, this is clearly a homework assignment. At least make an attempt and we can indicate errors, but don't blindly post without any effort of your own - it's pointless and wastes everyone's time.
Writing the question on this forum took more effort than finding the answer and writing the code, as pointed out above - you've practically written the entire thing in English (or rather, the question setter did) - so it could hardly be any simpler!