Go Program to Check Even or Odd Number
In Go, you can check if a number is even or odd by using the modulus operator
% like this: if num % 2 == 0 { /* even */ } else { /* odd */ }.Examples
Input4
Output4 is even
Input7
Output7 is odd
Input0
Output0 is even
How to Think About It
To check if a number is even or odd, divide it by 2 and look at the remainder. If the remainder is 0, the number is even because it divides evenly by 2. If the remainder is 1, the number is odd because it leaves a leftover when divided by 2.
Algorithm
1
Get the input number.2
Calculate the remainder when the number is divided by 2 using the modulus operator.3
If the remainder is 0, the number is even.4
Otherwise, the number is odd.5
Print the result.Code
go
package main import "fmt" func main() { var num int fmt.Print("Enter a number: ") fmt.Scan(&num) if num%2 == 0 { fmt.Printf("%d is even\n", num) } else { fmt.Printf("%d is odd\n", num) } }
Output
Enter a number: 7
7 is odd
Dry Run
Let's trace the input 7 through the code
1
Input number
User enters 7, so num = 7
2
Calculate remainder
7 % 2 = 1 (since 7 divided by 2 leaves remainder 1)
3
Check remainder
Since remainder is 1, condition num%2 == 0 is false
4
Print result
Print '7 is odd'
| num | num % 2 | Condition (num % 2 == 0) | Output |
|---|---|---|---|
| 7 | 1 | false | "7 is odd" |
Why This Works
Step 1: Using modulus operator
The % operator gives the remainder of division, which helps us find if a number divides evenly by 2.
Step 2: Even number check
If the remainder is 0, the number is even because it divides exactly by 2.
Step 3: Odd number check
If the remainder is not 0, the number is odd because it leaves a remainder when divided by 2.
Alternative Approaches
Using bitwise AND operator
go
package main import "fmt" func main() { var num int fmt.Print("Enter a number: ") fmt.Scan(&num) if num&1 == 0 { fmt.Printf("%d is even\n", num) } else { fmt.Printf("%d is odd\n", num) } }
This method uses bitwise AND to check the least significant bit; it's faster but less intuitive for beginners.
Using a function to check even or odd
go
package main import "fmt" func isEven(num int) bool { return num%2 == 0 } func main() { var num int fmt.Print("Enter a number: ") fmt.Scan(&num) if isEven(num) { fmt.Printf("%d is even\n", num) } else { fmt.Printf("%d is odd\n", num) } }
This approach separates logic into a function, improving code reuse and clarity.
Complexity: O(1) time, O(1) space
Time Complexity
The check uses a single modulus operation which takes constant time, so the time complexity is O(1).
Space Complexity
The program uses a fixed amount of memory for variables, so space complexity is O(1).
Which Approach is Fastest?
Using bitwise AND is slightly faster than modulus but both are constant time and efficient for this task.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Modulus operator (%) | O(1) | O(1) | Simplicity and clarity |
| Bitwise AND (&) | O(1) | O(1) | Performance and low-level operations |
| Function abstraction | O(1) | O(1) | Code reuse and readability |
Use
num % 2 == 0 to quickly check if a number is even in Go.Beginners often forget to use the modulus operator and try to compare the number directly to 2.