Golang Program to Find the Gravitational Force Acting Between Two Objects

Go ProgrammingServer Side ProgrammingProgramming

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

 Live Demo

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
raja
Updated on 31-Jul-2021 15:53:38

Advertisements