Language
C++
Compiler
gcc 12.1.0
Options
Warnings
Don't Use Boost
C++11
-pedantic
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
const double G = 6.67408e-11;
double r1, r2, m1, m2, x;
char burh;
cout << "Enter the mass and radius of sphere 1 (m1, r1): ";
cin >> m1 >> burh >>r1;
cout << "Enter the mass and radius of sphere 2 (m2, r2): ";
cin >> m2 >> burh >> r2;
while (m1 < 0 || r1 < 0 || m2 < 0 || r2 < 0) {
cout << "Error: Mass and radius values must be greater than zero, Please re-enter the values." << endl;
cout << "Enter the mass and radius of sphere 1 (m1, r1): ";
cin >> m1 >> burh >> r1;
cout << "Enter the mass and radius of sphere 2 (m2, r2): ";
cin >> m2 >> burh >> r2;
}
cout << "Enter the distance between the two spheres (x): ";
cin >> x;
while (x < 0) {
cout << "Error: Distance value must be greater than zero, Please re-enter the value." << endl;
cout << "Enter the distance between the two spheres (x): ";
cin >> x;
}
cout << "Distance (m)" << setw(20) << "Force (N)" << endl;
double d = x + r1 + r2;
double force = (m1 * m2 * G) / (d * d);
cout << fixed << setprecision(2) << setw(10) << d << setw(20) << force << endl;
for (int i = 1; i <= 20; i++) {
x -= x / 20;
d = x + r1 + r2;
force = (m1 * m2 * G) / (d * d);
cout << fixed << setprecision(2) << setw(10) << d << setw(20) << force << endl;
}
return 0;
}
1000000000,1
1000000000,1
1
$ g++ prog.cc -Wall -Wextra -std=c++11 -pedantic
Enter the mass and radius of sphere 1 (m1, r1): Enter the mass and radius of sphere 2 (m2, r2): Enter the distance between the two spheres (x): Distance (m) Force (N)
3.00 7415644.44
2.95 7669152.54
2.90 7922220.96
2.86 8174419.60
2.81 8425331.03
2.77 8674552.70
2.74 8921698.95
2.70 9166402.76
2.66 9408317.27
2.63 9647116.97
2.60 9882498.77
2.57 10114182.64
2.54 10341912.20
2.51 10565454.93
2.49 10784602.24
2.46 10999169.30
2.44 11208994.74
2.42 11413940.11
2.40 11613889.30
2.38 11808747.68
2.36 11998441.31
Exit Code:
0