如何在 Golang 中檢查一個數是奇數還是偶數?
在本教程中,我們將學習如何檢查一個數字是偶數還是奇數。能被 2 整除的數是偶數,不能被 2 整除的數是奇數。
本教程包含三種不同的方法來實現這一點。
取模運算子 − 在第一種方法中,我們使用取模 (%) 運算子。此運算子用於查詢兩個數的餘數。在我們的例子中,我們將對數字進行模 2 運算,如果返回 0,則該數字為偶數,否則為奇數。
按位運算子 − 第二種方法是使用按位運算子 &。對於偶數,二進位制表示中的最後一位是 0。例如,2 的二進位制表示是 10,4 是 100,等等。而在奇數的二進位制表示中,最後一位是 1,例如 3 的二進位制表示是 11,5 是 110,等等。利用這個概念,我們可以對數字和 1 進行 & 運算,如果返回 1,則該數字為奇數,否則為偶數。
遞迴函式 − 第三種方法將是遞迴函式,在每次函式呼叫中,我們從數字中減去 2,如果它變成零,則該數字為偶數,否則如果它變成 1,則該數字為奇數。
方法 1
在這種方法中,我們將使用取模算術運算子來檢查數字是偶數還是奇數。如果對數字取模得到 0,則該數字為偶數,否則為奇數。
語法
我們將使用取模運算子來檢查數字是偶數還是奇數,語法如下。
If number%2 == 0 {}
演算法
步驟 1:number := 10 − 使用 Golang 中的簡寫方法宣告並初始化變數。
步驟 2:if number%2 == 0 { } − 使用取模運算子檢查數字返回 0 或 1。
步驟 3:相應地列印結果
示例
package main
import (
// fmt package provides the function to print anything
"fmt"
)
func main() {
// declaring and initializing the variable using the shorthand method in Golang
number := 10
fmt.Println("Golang program to check that the number is even or odd using the modulus Relational operator.")
// using the % operator and using the if else block accordingly
if number%2 == 0 {
fmt.Printf("The number %d is Even.\n", number)
} else {
fmt.Printf("The number %d is Odd.\n", number)
}
}
輸出
Golang program to check that the number is even or odd using the modulus Relational operator. The number 10 is Even.
方法 2
在這種方法中,我們將使用按位運算子來檢視數字是奇數還是偶數。眾所周知,對於每個偶數,其二進位制形式的最後一位將為 0。如果我們用 1 執行按位 & 運算並且它返回 0,則它將是偶數。
演算法
步驟 1:number := 10 − 使用 Golang 中的簡寫方法宣告並初始化變數。
步驟 2:if number & 1 == 0 { } − 使用按位 & 運算子檢查數字返回 0 或 1。
步驟 3:相應地列印結果。
示例
package main
import (
// fmt package provides the function to print anything
"fmt"
)
func main() {
// declaring and initializing the variable using the shorthand method in Golang
number := 9
fmt.Println("Golang program to check that the number is even or odd using the bitwise & operator.")
// using the & operator and using the if else block accordingly
if number&1 == 0 {
fmt.Printf("The number %d is Even.\n", number)
} else {
fmt.Printf("The number %d is Odd.\n", number)
}
}
輸出
Golang program to check that the number is even or odd using the bitwise & operator. The number 9 is odd.
方法 3
在這種方法中,我們建立了一個遞迴函式,該函式將引數中的數字減少 2。如果數字變為零,則返回 true,這意味著該數字為偶數,否則如果它變為 1,則返回 false。
語法
呼叫程式本身中建立的 evenOrOdd() 函式。
func evenOrOdd(number int) bool { }
演算法
步驟 1:number := 10 − 使用 golang 中的簡寫方法宣告並初始化變數。
步驟 2:if evenOrOdd(number) { } − 呼叫函式並將數字作為引數傳遞以檢查它是偶數還是奇數。
步驟 3:相應地列印結果。
示例
package main
import (
// fmt package provides the function to print anything
"fmt"
)
// defining the recursive function to check that the number is even or odd
func evenOrOdd(number int) bool {
// checking that the number reached to zero or not
// if yes then the number is even
if number == 0 {
return true
}
// if the number becomes 1 then the number is odd
if number == 1 {
return false
}
// calling the recursive function by passing the number 2 less than before
return evenOrOdd(number - 2)
}
func main() {
// declaring and initializing the variable using the shorthand method in Golang
number := 10
fmt.Println("Golang program to check that the number is even or odd using a recursive function.")
// using the & operator and using the if else block accordingly
if evenOrOdd(number) {
fmt.Printf("The number %d is Even.\n", number)
} else {
fmt.Printf("The number %d is Odd.\n", number)
}
}
輸出
Golang program to check that the number is even or odd using a recursive function. The number 10 is Even.
結論
這三種方法可以檢查數字是偶數還是奇數。第一種方法使用取模運算子,第二種方法使用按位運算子,在時間複雜度、模組化和程式碼可重用性方面更合適。要了解更多關於 Go 的資訊,您可以探索這些 教程。
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP