Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Program to find gravitational force between two objects

  • Last Updated : 09 Jul, 2021

Introduction to Gravitational Force

We know that gravity is universal. According to Newton’s Law of Universal Gravitation, all objects attract each other with a force of gravitational attraction. According to this law, the force of gravitational attraction is directly dependent upon the masses of both objects and inversely proportional to the square of the distance that separates their centres. On removing the proportionality sign, we add G, the Universal Gravitational Constant. This can be reduced to the following equation: 
{\displaystyle F=G\left ( \frac {m_{1}m_{2}}{r^2} \right )}
F= Gravitation force of attraction between two objects in newton(N)
G=Universal Gravitational Constant(  6.67*10^{-11} N-m^2/kg^2  )
m1 and m2 = Mass of two objects(Kg)
r= separation in meters(m) between the objects, as measured from their centres of mass.

Attention reader! All those who say programming isn't for kids, just haven't met the right mentors yet. Join the  Demo Class for First Step to Coding Coursespecifically designed for students of class 8 to 12. 

The students will get to learn more about the world of programming in these free classes which will definitely help them in making a wise career choice in the future.

Examples: 



Input: m1 = 20000000 kg
       m2 = 4000000 kg
       r = 15 m
Output : The Gravitational Force is:  23.73 N

Input: m1 = 5000000 kg
       m2 = 900000 kg
       r = 30 m
Output : The Gravitational Force is:  0.33 N

The approach is simple. We will just use the formula mentioned in the Introduction. 

C++




// C++ code to find Gravitational Force
#include <iostream>
using namespace std;
 
float round(float F)
{
    float value = (int)(F * 100 + .5);
    return (float)value / 100;
}
 
float force(double m1, double m2, double r)
{
    float G;
    G = 6.67 / 1e11;
    float F;
    F = (G * m1 * m2) / (r * r);
     
    // Rounding to two digits after decimal
    return round(F);
}
 
// Driver code
int main()
{
    float m1, m2, r;
    m1 = 5000000;
    m2 = 900000;
    r = 30;
     
    cout << "The Gravitational Fource is: "
         << force(m1, m2, r) << "N";
     
    return 0;
}
 
// This code is contributed by parthagarwal1962000

Python3




# Python3 code to find Gravitational Force
def force(m1, m2, r):
    G = 6.673*(10**-11)
    F = (G*m1*m2)/(r**2)
 
    # Rounding to two digits after decimal
    return round(F, 2)
 
# Driver Code
m1 = 5000000
m2 = 900000
r = 30
print("The Gravitational Force is: ", force(m1, m2, r), "N")

Javascript




<script>
// JavaScript code to find Gravitational Force
function round (F)
{
    var value = (F * 100 + .5);
     
    return value / 100;
    
}
 
function force(m1, m2, r)
{
    var G;
     G = 6.67 / 1e11;
    var F;
    F = (G * m1 * m2) / (r * r);
     
         
    // Rounding to two digits after decimal
    return round(F);
     
}
 
// Driver code
    var m1, m2, r;
    m1 = 5000000;
    m2 = 900000;
    r = 30;
     
    document.write("The Gravitational Fource is: " + force(m1, m2, r) +"N");
     
// This code is contributed by shivanisinghss2110
</script>

Output: 

The Gravitational Force is:  0.33 N

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :