Although your solution, Chiranjeevi Kanthraj, is not correct, I shall refer you to what it says on the title page for
this forum:
We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
Don't be annoyed with me, but in line with that recommendation I have pulled rank and deleted most of the code you posted.
Please remember that posting a complete solution at an early stage is unlikely to help the poster to learn.