Go Program to Count Positive and Negative Numbers in an Array

In this Go Program to Count Positive and Negative Numbers in an Array, we used for loop to iterate the posNegArr array. The if condition (if posNegArr[i] >= 0) checks the array element is greater than or equal to zero. If True, we increment the positive count (positiveCount++); otherwise, increment the (negativeCount++) negative count value.

package main

import "fmt"

func main() {
    var size, i int

    fmt.Print("Enter the Positive Negative Array Size = ")
    fmt.Scan(&size)

    posNegArr := make([]int, size)

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

    positiveCount := 0
    negativeCount := 0

    for i = 0; i < size; i++ {
        if posNegArr[i] >= 0 {
            positiveCount++
        } else {
            negativeCount++
        }
    }
    fmt.Println("The Total Number of Positive Numbers = ", positiveCount)
    fmt.Println("The Total Number of Negative Numbers = ", negativeCount)
}
Enter the Positive Negative Array Size = 5
Enter the Positive Negative Array Items  = 1 -2 -3 0 -5
The Total Number of Positive Numbers =  2
The Total Number of Negative Numbers =  3

Golang Program to Count Positive and Negative Numbers in an Array using the for loop range.

package main

import "fmt"

func main() {
    var size, i int

    fmt.Print("Enter the Positive Negative Array Size = ")
    fmt.Scan(&size)

    posNegArr := make([]int, size)

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

    positiveCount := 0
    negativeCount := 0

    for _, pn := range posNegArr {
        if pn >= 0 {
            positiveCount++
        } else {
            negativeCount++
        }
    }
    fmt.Println("The Total Number of Positive Numbers = ", positiveCount)
    fmt.Println("The Total Number of Negative Numbers = ", negativeCount)
}
Go Program to Count Positive and Negative Numbers in an Array 2

In this Go array example, we created two separate functions (func countPositiveNums & countNegativeNums) that return the count of positive and negative numbers.

package main

import "fmt"

var positiveCount, negativeCount int

func countPositiveNums(posNegArr []int) int {
    positiveCount = 0
    for _, pn := range posNegArr {
        if pn >= 0 {
            positiveCount++
        }
    }
    return positiveCount
}
func countNegativeNums(posNegArr []int) int {
    negativeCount = 0
    for _, pn := range posNegArr {
        if pn < 0 {
            negativeCount++
        }
    }
    return negativeCount
}
func main() {
    var size int

    fmt.Print("Enter the positive negative Array Size = ")
    fmt.Scan(&size)

    posNegArr := make([]int, size)

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

    positiveCount = countPositiveNums(posNegArr)
    negativeCount = countNegativeNums(posNegArr)
    fmt.Println("The Total Number of positive Numbers = ", positiveCount)
    fmt.Println("The Total Number of negative Numbers = ", negativeCount)
}
Enter the positive negative Array Size = 9
Enter the positive negative Array Items  = -2 -3 0 -9 11 22 33 -88 100
The Total Number of positive Numbers =  5
The Total Number of negative Numbers =  4