Go Language - Program to Check Whether a Number is Odd or Even

Hello everyone, today we will learn how to check whether the entered number is even or odd. Even numbers are the numbers that are divisible by 2 and the numbers that are not divisible by 2 are called odd numbers. Here, in this program, we will check whether the number is divisible by 2 or not. If divisible, then it is an even number, and if not then it is an odd number.

Example 1: Check Whether the Number is Even or Odd

package main

import (
"fmt"
)

func main() {
var number int
fmt.Println("Enter an integer value : ")

_, err := fmt.Scanf("%d", &number)

if err != nil {
fmt.Println(err)
}

CheckOddOrEven(number)
}

func CheckOddOrEven(number int) {

//If number is divisible by 2 then it's an even number
//else odd number
if number%2 == 0 {

fmt.Println("The entered number ", number, " is Even")

} else {

fmt.Println("The entered number ", number, " is Odd")

}
}

Output:




Example 2:  Check whether the number is even or odd by using the bitwise XOR

package main

import (
"fmt"
)

func main() {
var number int
fmt.Println("Enter an integer value : ")

_, err := fmt.Scanf("%d", &number)

if err != nil {
fmt.Println(err)
}

CheckOddOrEven(number)
}

func CheckOddOrEven(number int) {

//Check Using Bitwise XOR
if (number ^ 1) == number+1 {

fmt.Println("The entered number ", number, " is Even")

} else {

fmt.Println("The entered number ", number, " is Odd")

}
}

Output:

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Spring boot video streaming example-HTML5

Spring Boot + Mockito simple application with 100% code coverage

Spring Boot + OpenCSV Export Data to CSV Example

Custom Exception Handling in Quarkus REST API

Registration and Login with Spring Boot + Spring Security + Thymeleaf

DataTable-Pagination example with Spring boot, jQuery and ajax

Spring Webflux File Download - REST API Example

Node JS mini projects with source code - free download

ReactJS, Spring Boot JWT Authentication Example