The production of the X(3872) is studied in pp collisions at √s = 7TeV, using decays to J/ψπ+π−, where the J/ψ decays to two muons. The data were recorded by the CMS experiment and correspond to an integrated luminosity of 4.8fb−1. The measurements are performed in a kinematic range in which the X(3872) candidates have a transverse momentum 10 < pT < 50GeV and rapidity |y| < 1.2. The ratio of the X(3872) and ψ (2S) cross sections times their branching fractions into J/ψπ+π− is measured as a function of pT. In addition, the fraction of X(3872) originating from B decays is determined. From these measurements the prompt X(3872) differential cross section times branching fraction as a function of pT is extracted. The π+π− mass spectrum of the J/ψπ+π− system in the X(3872) decays is also investigated.