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

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete