Python Program to find the gravitational force between two objects


March 8, 2022, Learn eTutorial
1389

In this simple python program, we need to calculate the gravitational force. It is a beginner-level python program.

For a better understanding of this example, we always recommend you to learn the basic topics of Python programming listed below:

What is called gravity?

Gravity is the force that attracts every object to the earth, in this python program we need to calculate the gravitational force between two bodies. Gravitational force act only on the center of the mass of the objects. The force of gravity between two objects is inversely proportional to the square of the distance between them. Here in this python gravitational problem, we need to check the force between the objects which can be found by using the formula.

F = f=(G*m1*m2)/(r**2), where the

  • G is the gravitational constant, which is equal to 6.673*(10**-11).
  • m1 and m2 are the masses of the two objects.
  • r is the distance between the center of the masses.

How to find gravitational force in python?

To apply this logic in our python programming language, we accept the masses m1, m2, and the distance r from the user using float data type which supports the decimal values. Then we need to apply the formula F = f=(G*m1*m2)/(r**2). Before that, we need to calculate the  gravitational constant G. After calculating the gravitational force, and we print the result using format method to make the value precision of two decimal places using python syntax.

ALGORITHM

STEP 1: Accept the values of two masses and the distance between the center of the masses using the input method and convert that string to float using float data type in python programming.

STEP 2: Calculate the value of G the gravitational constant using the formula G = 6.673*(10**-11). This is needed for calculating the force.

STEP 3: Calculate the gravitational force using the formula f=(G*m1*m2)/(r**2), where m1 and m2 are the masses and r is the distance between the center of masses.

STEP 4:  Using python programming basics print the value of the gravitational force using the round method two make the result exactly two decimal values.

Python Source Code

                                          m1=float(input("Enter the first object: "))

m2=float(input("Enter the second object: "))

r=float(input("Enter the distance between the center of the objects: "))

G=6.673*(10**-11)

f=(G*m1*m2)/(r**2)

print("The gravitational force between objects is: ",round(f,2),"N")
                                      

OUTPUT

Enter the first object: 1000000
Enter the second object: 500000
Enter the distance between the center of the objects: 20
The gravitational force between objects is:  0.08 N