as far as I can tell the while loop inside for works (meaning it finally ends at some point and the program terminates). The problem is the condition for printing out res: there have to be both of your conditions true to output the result. The second one ((res==mul)) is never fulfilled so the while just loops until it gets a really small value that considers 0 and moves on.