Go Program to Put Positive and Negatives in a Separate Array

Write a Go Program to Put Positive and Negative numbers in a Separate Array using for loop. First, we declared three arrays. The if condition (if posNegArr[i] >= 0) checks whether the array item is greater than or equal to zero within the loop. If True, we assigned that value to posArr (posArr[positiveCount] = posNegArr[i]) and incremented the positive count value. Otherwise, set the value to NegArr (NegArr[negativeCount] = posNegArr[i]) and increment the negative count value. Next, we created a function (printArray(posNegArr []int, size int)) that prints the positive and negative array items.

package main

import "fmt"

func printArray(posNegArr []int, size int) {
    for i := 0; i < size; i++ {
        fmt.Print(posNegArr[i], " ")
    }
}
func main() {
    var size, i int

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

    posNegArr := make([]int, size)
    posArr := make([]int, size)
    NegArr := 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 {
            posArr[positiveCount] = posNegArr[i]
            positiveCount++
        } else {
            NegArr[negativeCount] = posNegArr[i]
            negativeCount++
        }
    }
    fmt.Println("The Total Number of Positive Numbers = ", positiveCount)
    fmt.Print("The Positive Array Elements are = ")
    printArray(posArr, positiveCount)
    fmt.Println("The Total Number of Negative Numbers = ", negativeCount)
    fmt.Print("The Negative Array Elements are = ")
    printArray(NegArr, negativeCount)
}
Go Program to Put Positive and Negatives in Separate Array 1

Golang Program to Put Positive and Negative Numbers in a Separate Array

In this Go example, we created two separate functions (putPositiveNums and putNegativeNums) that place the positive numbers in the positive and negative numbers in the negative array.

package main

import "fmt"

var positiveCount, negativeCount, i int

func printArray(posNegArr []int, size int) {
    for i = 0; i < size; i++ {
        fmt.Print(posNegArr[i], " ")
    }
}
func putPositiveNums(posNegArr []int, size int) {
    posArr := make([]int, size)
    positiveCount = 0
    for i = 0; i < size; i++ {
        if posNegArr[i] >= 0 {
            posArr[positiveCount] = posNegArr[i]
            positiveCount++
        }
    }
    fmt.Println("The Total Number of Positive Numbers = ", positiveCount)
    fmt.Print("The Positive Array Elements are = ")
    printArray(posArr, positiveCount)
}
func putNegativeNums(posNegArr []int, size int) {
    NegArr := make([]int, size)
    negativeCount = 0
    for i = 0; i < size; i++ {
        if posNegArr[i] < 0 {
            NegArr[negativeCount] = posNegArr[i]
            negativeCount++
        }
    }
    fmt.Println("\nThe Total Number of Negative Numbers = ", negativeCount)
    fmt.Print("The Negative Array Elements are = ")
    printArray(NegArr, negativeCount)
}
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])
    }

    putPositiveNums(posNegArr, size)
    putNegativeNums(posNegArr, size)
    fmt.Println()

}
Enter the Positive Negative Array Size = 8
Enter the Positive Negative Array Items  = 10 -22 -88 0 -77 5 -12 100
The Total Number of Positive Numbers =  4
The Positive Array Elements are = 10 0 5 100 
The Total Number of Negative Numbers =  4
The Negative Array Elements are = -22 -88 -77 -12