Generation of photon number states on demand