Let the particle of mass #m_1# is situateted at the orgin (0.0) and the other particle of mass #m_2# is on the x-axis at x distance appart from the first particle. The force of gravitational attraction on the 2nd particle is #F(x)=(Gm_1m_2)/x^2#
Where G = Gravitational constant.
The work done to displace the 2nd particle infinitesimilly small distance #dx# towards the first one is given by
#dW=F(x)dx#
So by integrating in the limit #(ootor)# we get the gravitational potential energy of the system when they are # r# distance appart
Hence
#W=int_(oo)^rdW=int_(oo)^rF(x)dx#
#=(Gm_1m_2)int_(oo)^r1/x^2dx#
#=-(Gm_1m_2)[1/x]_(oo)^r#
#=-(Gm_1m_2)/r#