Check for zero to avoid infinite loop
Fixed example program