in reply to Calculation loop

Even if this problem isn't homework (this is becoming a tired phrase...), you might benefit from tackling this problem yourself.

Split the problem into pieces.

My solution is a little messy, but I'll give you a few hints.