From the fact that he uses Dev C++ I deduct that this is a homework question, and I don't really know if you are allowed to use the clever solution in homeworks Athar.
Probably not, but I wouldn't post a solution that could be handed in as homework without problems anyway.
Certainly not without some effort on OP's part.