No need for nested loops. If you do insist on nesting the loops, reset denom to 1 at the beginning of the while loop.