The shape of the stellar initial mass function (IMF) of star clusters has important consequences for the subsequent evolution of the clusters. In this paper we examine a star formation scenario in which the IMF is determined. Thermal and dynamical instability result in the fragmentation of a parent gaseous protocluster cloud into cold, dense, low-mass cloudlets. Here we examine the subsequent evolution of the cloudlets as the cluster approaches virial equilibrium. Because of their inverse buoyancy,the cloudlets fall toward the central region of the protocluster cloud. During the infall, cohesive collisions cause the cloudlets' masses to grow. When the mass of a cloudlet exceeds the critical mass for gravitational instability, M(G), it collapses to form a protostellar core. Its mass may continue to grow as a result of mergers with remaining cloudlets until its UV emission heals and ionizes nearby cloudlets. The most massive stars require many dissipative mergers and so are preferentially formed in the cluster center, giving an initial mass segregation consistent with the observed stellar distribution in open clusters. Energy loss associated with the mergers also makes it more likely that the newly formed clusters will remain gravitationally bound even in the limit of inefficient star formation. The coagulation process naturally leads to a power-law IMF. The range of power-law exponents, x is found to be similar to those observed in both open and globular star clusters in the Galaxy. Although limited by the use of a direct N-body code to initial particle numbers N-i less than or similar to 10(4) and final number of stars less than or similar to 10(3), the results are found to be insensitive to N-i. for a constant value of the initial covering factor of the stars. The results can therefore be confidently applied to very rich clusters. The final slope of the IMF also depends upon the initial velocity distribution and the ratio of the initial mass of the cloudlets to M(G). The latter ratio may be a function of the metallicity ([Fe/H]) and external pressure of the protocluster clouds so as to give the observed variation of x with [Fe/H] and Galactocentric position among the Galactic globular clusters.