# 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: F= Gravitation force of attraction between two objects in newton(N)
G=Universal Gravitational Constant( )
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 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 codeint 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 Forcedef 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 Codem1 = 5000000m2 = 900000r = 30print("The Gravitational Force is: ", force(m1, m2, r), "N")

## Javascript

 

Output:

The Gravitational Force is:  0.33 N

My Personal Notes arrow_drop_up