使用庫函式獲取整數前驅的 Go 語言程式
本文將討論如何在 Go 語言中使用庫函式獲取整數的前驅。
整數的前驅定義為其前面的數。例如,2 的前驅是 2 – 1 = 1。類似地,-50 的前驅是 -51,依此類推。
語法
Syntax for Println() = fmt.Println(...interface{})
示例 1:我們將使用庫函式獲取整數的前驅
演算法
步驟 1 − 匯入 fmt 包。
步驟 2 − 啟動 main 函式。
步驟 3 − 初始化一個 int 型別變數並存儲值。
步驟 4 − 實現查詢前驅的邏輯。
步驟 5 − 在螢幕上列印結果。
示例
// GOLANG PROGRAM TO FIND THE PREDECESSOR OF AN INTEGER NUMBER USING LIBRARY // fmt package allows us to print anything on the screen. package main import "fmt" // fmt package allows us to print anything on the screen. // calling the main() function func main() { fmt.Println("Golang program to find the predecessor of an integer number using library function") // initializing the number variable to store the required number. var number, predecessor int // assigning value to the number variable. number = 49 // implementing the logic predecessor = number - 1 // Print the result fmt.Println(" predecessor of", number, "is ", predecessor ) }
輸出
Golang program to find the predecessor of an integer number using library function predecessor of 49 is 48
程式碼描述
首先,我們匯入 fmt 包,該包允許我們列印任何內容。
然後我們啟動main()函式。
之後,我們需要將數字賦給一個變數並建立一個前驅變數。
然後建立查詢前一個整數的邏輯。
使用fmt.Println()函式在螢幕上列印結果。
示例 2:我們將使用庫函式獲取整數的前驅,不帶任何引數且不返回值
演算法
步驟 1 − 匯入 fmt 包和包。
步驟 2 − 啟動main()函式。
步驟 3 − 呼叫predecessor()函式。
步驟 4 − 啟動predecessor()函式。
步驟 5 − 宣告並初始化變數。
步驟 6 − 使用fmt.Printf()在控制檯螢幕上列印結果。
示例
// GOLANG PROGRAM TO FIND THE PREDECESSOR OF AN INTEGER NUMBER USING LIBRARY FUNCTION // fmt package allows us to print anything on the screen. package main import "fmt" // function prototype // GO program execution starts with the function main() func main() { // function call predecessor() } func predecessor() { fmt.Println("Golang program to find the predecessor of an integer number using library function") // declare and initialize the variable var c int c = 4 // print the result fmt.Println("predeccessor of the number",c,"=",c - 1) }
輸出
Golang program to find the predecessor of an integer number using library function predeccessor of the number 4 = 3
程式碼描述
首先,我們匯入了 fmt 包,該包允許我們列印任何內容。
現在讓我們啟動function main()。GO 程式執行從 main() 函式開始。
接下來我們呼叫predecessor()函式。
現在我們啟動predecessor()函式。宣告並初始化整數變數 c,需要找到其前驅。
在上面的程式中,predecessor()函式執行計算,並且沒有引數傳遞給此函式。此函式的返回型別為空,因此沒有返回值。
最終結果使用內建函式 fmt.Printf() 在控制檯螢幕上列印。此函式在 fmt 包中定義,它有助於寫入標準輸出。
示例 3:我們將使用庫函式在兩個獨立的函式中獲取整數的前驅,帶引數和返回值
演算法
步驟 1 − 匯入 fmt 包
步驟 2 − 建立並定義predecessor()函式。
步驟 3 − 啟動 main函式()。
步驟 4 − 從使用者獲取值。
步驟 5 − 呼叫上面建立的predecessor()函式,並將使用者輸入的值傳遞給它。
步驟 6 − 在螢幕上列印結果。
示例
// GOLANG PROGRAM TO FIND THE PREDECESSOR OF AN INTEGER NUMBER USING LIBRARY // fmt package allows us to print anything on the screen. package main import "fmt" func predecessor(number int) int { var n int n = number - 1 return n } // calling the main() function func main() { fmt.Println("Golang program to find the predecessor of an integer number using library function") // declare the variables var number, previous int // initialize the number variable number = 66 // implementing the logic. // function call previous = predecessor(number) // print the result fmt.Println(" predecessor of", number, "is ", previous ) }
輸出
Golang program to find the predecessor of an integer number using library function predecessor of 66 is 65
程式碼描述
首先,我們匯入了 fmt 包,該包允許我們列印任何內容。
然後建立並定義前驅函式,該函式將返回使用者輸入的整數的前一個值。
然後我們啟動了main()函式。
我們宣告整數變數 number 和 previous。使用一個值初始化變數 number 以查詢其前驅並將結果儲存在變數 previous 中。
呼叫前驅函式並存儲答案。
最後使用fmt.Println()函式在螢幕上列印結果。
結論
我們已成功編譯並執行了 Go 語言程式,以使用庫函式查詢整數的前驅,並附帶示例。