程序找到两个物体之间的引力
万有引力简介
我们知道万有引力是普遍存在的。根据牛顿万有引力定律,所有物体都通过万有引力相互吸引。根据该定律,万有引力与两个物体的质量直接相关,与它们中心之间距离的平方成反比。在移除比例符号后,我们添加 G,即万有引力常数。这可以简化为以下等式:
F = 两个物体之间的万有引力,单位为牛顿(N)
G=万有引力常数( )
m1 和 m2 = 两个物体的质量(Kg)
r = 以米(m)为单位的物体之间的距离,从它们的质心开始测量。
例子:
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
方法很简单。我们将只使用引言中提到的公式。
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 code
int 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 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
输出:
The Gravitational Force is: 0.33 N