Open In App

Program to find gravitational force between two objects

Last Updated : 15 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

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 Force 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 Force is: " + force(m1, m2, r) +"N");
     
// This code is contributed by shivanisinghss2110
</script>

                    

Java

public class GravitationalForce {
    public static float round(float F) {
        float value = (int)(F * 100 + 0.5);
        return (float)value / 100;
    }
 
    public static float force(double m1, double m2, double r) {
        float G = (float) (6.67 / 1e11);
        float F = (G * (float) m1 * (float) m2) / ((float) r * (float) r);
 
        // Rounding to two digits after decimal
        return round(F);
    }
 
    // Driver code
    public static void main(String[] args) {
        float m1, m2, r;
        m1 = 5000000;
        m2 = 900000;
        r = 30;
 
        System.out.println("The Gravitational Force is: " + force(m1, m2, r) + "N");
    }
}

                    

C#

using System;
 
public class GravitationalForce {
    public static float Round(float F) {
        float value = (int)(F * 100 + 0.5);
        return (float)value / 100;
    }
 
    public static float Force(double m1, double m2, double r) {
        float G = (float) (6.67 / 1e11);
        float F = (G * (float) m1 * (float) m2) / ((float) r * (float) r);
 
        // Rounding to two digits after decimal
        return Round(F);
    }
 
    // Driver code
    public static void Main(string[] args) {
        float m1, m2, r;
        m1 = 5000000;
        m2 = 900000;
        r = 30;
 
        Console.WriteLine("The Gravitational Force is: " + Force(m1, m2, r) + "N");
    }
}

                    

Output
The Gravitational Force is: 0.33N


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads