Program to find gravitational force between two objects
Last Updated :
15 Mar, 2023
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.
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++
#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);
return round(F);
}
int main()
{
float m1, m2, r;
m1 = 5000000;
m2 = 900000;
r = 30;
cout << "The Gravitational Force is: "
<< force(m1, m2, r) << "N" ;
return 0;
}
|
Python3
def force(m1, m2, r):
G = 6.673 * ( 10 * * - 11 )
F = (G * m1 * m2) / (r * * 2 )
return round (F, 2 )
m1 = 5000000
m2 = 900000
r = 30
print ( "The Gravitational Force is: " , force(m1, m2, r), "N" )
|
Javascript
<script>
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);
return round(F);
}
var m1, m2, r;
m1 = 5000000;
m2 = 900000;
r = 30;
document.write( "The Gravitational Force is: " + force(m1, m2, r) + "N" );
</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);
return round(F);
}
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);
return Round(F);
}
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" );
}
}
|
OutputThe Gravitational Force is: 0.33N
Share your thoughts in the comments
Please Login to comment...