Not all planets have moons, and the number of moons around planets vary. The reason for the presence of a moon (or moons) around planets vary. In the case of the Earth and its Moon, the leading theory is that a planet-sized asteroid collided with the young Earth early on after it formed, and some of the ejecta from the collision got into orbit around the Earth and collapsed to form the Moon.
In the case of other planets / moons, it could be by gravitational ‘capture’.
Comments