In this paper an algorithm to deal with the singularity problem in the orbital mechanics is proposed. Lagrangian and Gaussian equations of motion are transformed into singularity-free ones by multiplying the so-called singular factors to the individual equations and the intermediate solutions can be derived by indefinite integration. Two criteria are introduced to decide how to transform the intermediate solutions into the solutions of the original problems inversely. Two examples, orbit solutions disturbed by the solar oblateness and the solar radiation pressure, are given to show the applicability of the algorithm. A similar method to solve the so-called critical inclination problem is also discussed with an example. Comments on the traditional variable transformations used to solve the singularity problem are also addressed.