- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang Program to Find the Gravitational Force Acting Between Two Objects
Steps
- Read both the masses and the distance between the masses and store them in separate variables.
- Initialize one of the variables to the value of gravitational constant, G.
- Then, the formula f=(G*m1*m2)/(r**2) is used to determine the force acting between the masses.
- Rounding off up to two decimal places, print the value of the force.
Enter the first mass: 1000000 Enter the second mass: 500000 Enter the distance between the centres of the masses: 20 Hence, the gravitational force is: 0.08 N | Enter the first mass: 90000000 Enter the second mass: 7000000 Enter the distance between the centres of the masses: 20 Hence, the gravitational force is: 105.1 N |
Explanation
- User must enter the values for both the masses and the distance between the masses and store them in separate variables.
- One of the variables is initialised to the value of gravitational constant (G) which is equal to 6.673*(10**-11).
- Then, the formula: f=(G*m1*m2)/(r**2), where m1 and m2 are the masses and r is the distance between them, is used to determine the magnitude of force acting between the masses.
- The force calculated is rounded up to 2 decimal places and printed.
Example
package main import ( "fmt" "math" ) func main(){ var a, b, r float64 fmt.Print("Enter the first mass: ") fmt.Scanf("%f", &a) fmt.Print("Enter the second mass: ") fmt.Scanf("%f", &b) fmt.Print("Enter the distance between the centres of the masses: ") fmt.Scanf("%f", &r) G:=6.673*(math.Pow(10, -11)) f:=(G*a*b)/(math.Pow(r, 2)) fmt.Printf("Hence, the gravitational force is: %.2f N", f) }
Output
Enter the first mass: 1000000 Enter the second mass: 500000 Enter the distance between the centres of the masses: 20 Hence, the gravitational force is: 0.08 N
Advertisements