How to Check Whether a Number is Even or Odd in Golang?


In this tutorial, we are going to learn how we can check whether the number is even or odd. The number that can be divisible by 2 is an even number and the number that is not divisible by two is an odd number.

This tutorial includes three different ways to achieve this

  • Moulous Operator − In the first method, we are using the modulus (%) operator. This operator is used to find the remainder of two numbers. In our case, we will do the modulus of the number with 2 and if it returns 0 then the number is even else the number is odd.

  • Bitwise operator − The second way is to use the bitwise operator &. For the even numbers, the last bit in the binary representation is 0. For example, the binary representation of 2 is 10, 4 is 100, etc. Whereas in the binary representation of odd numbers, the last bit in binary representation is 1 like the binary representation of 3 is 11, 5 is 110, etc. Using this concept we can do & of number and 1 if it returns 1 then the number is odd else even.

  • Recursive function − the third way will be the recursive function where on each function call we are reducing 2 from the number if it becomes zero then the number is even else if it becomes 1 then the number is odd.

Method 1

In this method, we are going to use the modulus arithmetic operator to check that the number is even or odd. If the modulus with the number gives 0 then the number is even else it is odd.

Syntax

We are going to use the modulus operator to check that the number is even or odd with the below syntax.

If number%2 == 0 {}

Algorithm

  • Step 1: number := 10 − Declare and initialize the variable using the shorthand method in Golang.

  • Step 2: if number%2 == 0 { } − Checking the number is returning 0 or 1 using the modulus operator.

  • Step 3: Print the result accordingly

Example

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)
    }
}

Output 

Golang program to check that the number is even or odd using the modulus Relational operator.
The number 10 is Even.

Method 2

In this method, we are going to use the bitwise operator to see if the number is odd or even. As we know that for every even number, the last bit of its binary form will be 0. If we perform bitwise & with 1 and it returns 0 then it will be an even number.

Algorithm

  • Step 1: number := 10 − Declare and initialize the variable using the shorthand method in Golang.

  • Step 2: if number & 1 == 0 { } − Checking the number is returning 0 or 1 using the bitwise & operator.

  • Step 3: Print the result accordingly.

Example

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)
    }
}

Output 

Golang program to check that the number is even or odd using the bitwise & operator.
The number 9 is odd.

Method 3

In this method, we are creating a recursive function which is reducing the number in the argument by 2. If the number becomes zero then it is returning true which means the number is even else if it becomes 1 then returning false.

Syntax

Calling evenOrOdd() function created in the program itself.

func evenOrOdd(number int) bool { }

Algorithm

  • Step 1: number := 10 − Declare and initialize the variable using the shorthand method in golang.

  • Step 2: if evenOrOdd(number) { } − Call the function and pass the number as a parameter to check if it is even or odd.

  • Step 3: Print the result accordingly.

Example

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)
    }
}

Output 

Golang program to check that the number is even or odd using a recursive function.
The number 10 is Even.

Conclusion

These are the three ways to check whether the number is even or odd. The first method where we are using the modulus operator, and the second method where we are using the bitwise operator are more suitable in terms of time complexity, modularity, and code reusability. To learn more about Go you can explore these tutorials.

Updated on: 10-Jul-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements