How to Calculate Gravitational Force Between Two Objects in Python


Here this example will tell us how to calculate the gravitational force between two objects in Python.

Source Code

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

m1 = float(input("Please input the mass of the first object (in kilogram): "))
m2 = float(input("Please input the mass of the second object (in kilogram): "))
r = float(input("Please input the distance between them (in meter): "))
G = 6.67408 * (10 ** -11)
f = G * m1 * m2 /(r ** 2)
print("The gravitational force between them is: ", round(f, 4), "N")

Output:

Please input the mass of the first object (in kilogram): 10000000
Please input the mass of the second object (in kilogram): 80000000
Please input the distance between them (in meter): 100
The gravitational force between them is: 5.3393 N

Please input the mass of the first object (in kilogram): 900000000
Please input the mass of the second object (in kilogram): 800000
Please input the distance between them (in meter): 1000
The gravitational force between them is: 0.0481 N
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments