We have measured optical second-harmonic generation (SHG) from atomically thin samples of MoS2 and h-BN with one to five layers. We observe strong SHG from materials with odd layer thickness, for which a noncentrosymmetric structure is expected, while the centrosymmetric materials with even layer thickness do not yield appreciable SHG. SHG for materials with odd layer thickness was measured as a function of crystal orientation. This dependence reveals the rotational symmetry of the lattice and is shown to provide a purely optical method of determining the orientation of crystallographic axes. We report values for the nonlinearity of monolayers and odd-layers of MoS2 and h-BN and compare the variation as a function of layer thickness with a model that accounts for wave propagation effects.