Go Program to Count Even and Odd Numbers in an Array

In this Go Program to Count Even and Odd Numbers in an Array, we used for loop to iterate the array. The if condition (if evoddarr[i]%2 == 0) checks the array element is divisible by two equals to zero. If True, we increment the even count (evenCount++); otherwise, increment the odd count value (oddCount++).

package main

import "fmt"

func main() {
    var size, i int

    fmt.Print("Enter the Even Odd Array Size = ")
    fmt.Scan(&size)

    evoddarr := make([]int, size)

    fmt.Print("Enter the Even Odd Array Items  = ")
    for i = 0; i < size; i++ {
        fmt.Scan(&evoddarr[i])
    }

    evenCount := 0
    oddCount := 0

    for i = 0; i < size; i++ {
        if evoddarr[i]%2 == 0 {
            evenCount++
        } else {
            oddCount++
        }
    }
    fmt.Println("The Total Number of Even Numbers = ", evenCount)
    fmt.Println("The Total Number of Odd Numbers  = ", oddCount)
}
Enter the Even Odd Array Size = 5
Enter the Even Odd Array Items  = 1 2 3 4 5
The Total Number of Even Numbers =  2
The Total Number of Odd Numbers  =  3

Golang Program to Count Even and Odd Numbers in an Array using the for loop range.

package main

import (
    "fmt"
)

func main() {
    var size int

    fmt.Print("Enter the Even Odd Array Size = ")
    fmt.Scan(&size)

    evoddarr := make([]int, size)

    fmt.Print("Enter the Even Odd Array Items  = ")
    for i := 0; i < size; i++ {
        fmt.Scan(&evoddarr[i])
    }

    evenCount := 0
    oddCount := 0

    for _, a := range evoddarr {
        if a%2 == 0 {
            evenCount++
        } else {
            oddCount++
        }
    }
    fmt.Println("The Total Number of Even Numbers = ", evenCount)
    fmt.Println("The Total Number of Odd Numbers  = ", oddCount)
}
Go Program to Count Even and Odd Numbers in an Array 2

In this Go even-odd array example, we created two separate functions (func countEvenNums(evoddarr []int) and func countOddNums) that return the count of Even and Odd numbers.

package main

import "fmt"

var evenCount, oddCount int

func countEvenNums(evoddarr []int) int {
    evenCount = 0
    fmt.Print("\nList of Even Numbers  = ")
    for _, a := range evoddarr {
        if a%2 == 0 {
            fmt.Print(a, " ")
            evenCount++
        }
    }
    return evenCount
}
func countOddNums(evoddarr []int) int {
    oddCount = 0
    fmt.Print("\nList of Odd Numbers   = ")
    for _, a := range evoddarr {
        if a%2 != 0 {
            fmt.Print(a, " ")
            oddCount++
        }
    }
    return oddCount
}
func main() {
    var size int

    fmt.Print("Enter the Even Odd Array Size = ")
    fmt.Scan(&size)

    evoddarr := make([]int, size)

    fmt.Print("Enter the Even Odd Array Items  = ")
    for i := 0; i < size; i++ {
        fmt.Scan(&evoddarr[i])
    }

    evenCount = countEvenNums(evoddarr)
    oddCount = countOddNums(evoddarr)
    fmt.Println("\nThe Total Number of Even Numbers = ", evenCount)
    fmt.Println("The Total Number of Odd Numbers  = ", oddCount)
}
Enter the Even Odd Array Size = 8
Enter the Even Odd Array Items  = 11 22 8 33 98 9 19 0

List of Even Numbers  = 22 8 98 0 
List of Odd Numbers   = 11 33 9 19 
The Total Number of Even Numbers =  4
The Total Number of Odd Numbers  =  4