如何在 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 的信息,您可以探索这些 教程

更新于: 2023年7月10日

3K+ 浏览量

开启您的 职业生涯

通过完成课程获得认证

开始学习
广告
© . All rights reserved.