Try to build logic and if it do not workout post the algorithm here we will try to help you.Building logic is the very important part of programming.so try yourself.