Golang 如何检查一个数字是偶数还是奇数

Golang 如何检查一个数字是偶数还是奇数

在本教程中,我们将学习如何检查一个数字是偶数还是奇数。能够被2整除的数字是偶数,不能被2整除的数字是奇数。

本教程包括三种不同的方式来实现这一点。

  • 模运算符 − 在第一种方法中,我们使用模运算符(%)。该运算符用于找到两个数字的余数。在我们的情况下,我们将用数字与2进行模运算,如果结果为0,则该数字为偶数,否则为奇数。

  • 位运算符 − 第二种方法是使用位运算符 &。对于偶数,二进制表示中的最后一位为0。例如,2的二进制表示为10,4的二进制表示为100,等等。而对于奇数的二进制表示,最后一位为1,例如3的二进制表示为11,5的二进制表示为110,等等。利用这个概念,我们可以对数字与1进行位与(&)运算,如果结果为1,则该数字为奇数,否则为偶数。

  • 递归函数 − 第三种方式是使用递归函数,在每次函数调用时,我们从数字中减去2,如果结果变为零,则该数字为偶数,否则如果结果变为1,则该数字为奇数。

方法1

在这种方法中,我们将使用模运算符来检查数字是偶数还是奇数。如果数字与2进行模运算的结果为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的内容,你可以浏览这些教程。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程