We describe the preparation of atom-number states with strongly interacting bosons in one dimension or
spin-polarized fermions. The procedure is based on a combination of weakening and squeezing of the trapping
potential. For the resulting state, the full atom-number distribution is obtained. Starting with an unknown
number of particles Ni, we optimize the sudden change in the trapping potential which leads to the Fock state
of Nf particles in the final trap. Nonzero temperature effects as well as different smooth trapping potentials are
analyzed. A simple criterion is provided to ensure the robust preparation of the Fock state for physically
realistic traps.