Go 語言程式:計算兩個物體之間的萬有引力
步驟
- 讀取兩個物體的質量和它們之間的距離,並將它們分別儲存在不同的變數中。
- 將其中一個變數初始化為萬有引力常數 G 的值。
- 然後,使用公式 f=(G*m1*m2)/(r**2) 計算作用在物體之間的力。
- 將計算出的力四捨五入到小數點後兩位,並列印結果。
| 輸入第一個物體的質量:1000000 輸入第二個物體的質量:500000 輸入兩個物體中心之間的距離:20 因此,萬有引力為:0.08 N | 輸入第一個物體的質量:90000000 輸入第二個物體的質量:7000000 輸入兩個物體中心之間的距離:20 因此,萬有引力為:105.1 N |
解釋
- 使用者需要輸入兩個物體的質量和它們之間的距離,並將它們儲存在不同的變數中。
- 其中一個變數被初始化為萬有引力常數 (G) 的值,其值為 6.673*(10**-11)。
- 然後,使用公式:f=(G*m1*m2)/(r**2),其中 m1 和 m2 是兩個物體的質量,r 是它們之間的距離,計算作用在物體之間的力的量級。
- 計算出的力四捨五入到小數點後 2 位並列印。
示例
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)
}輸出
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
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP