Tutorial Gateway

  • C
  • C#
  • Python
  • SQL
  • Java
  • JS
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs
  • MySQL

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()

}
Go Program to Put Positive and Negatives in Separate Array 2

Filed Under: Go Examples

  • Golang Hello World Program
  • Go Add Two Number
  • Go Calculate Electricity Bill
  • Go Calculate Employee Salary
  • Go Compound Interest
  • Go Count Digits in a Number
  • Go Count Total Notes in Amount
  • Go Cube of a Number
  • Go Even or Odd
  • Go Factorial of a Number
  • Go First Digit of a Number
  • Go Generic Root of a Number
  • Go Largest of Three Numbers
  • Go Leap year Program
  • Go Reverse Natural Numbers
  • Go Roots of a Quadratic Equation
  • Go Multiplication Table
  • Go Number divisible by 5 and 11
  • Go NCR Factorial of a Number
  • Go Positive or Negative
  • Go Power of a Number
  • Go Print Natural Numbers
  • Go Profit or Loss
  • Go Print 1 to 100 without loop
  • Go Program to Print 1 to 100
  • Go Product of Digits in a Number
  • Go Palindrome Number Program
  • Go Print Even Numbers
  • Go Print Odd Numbers
  • Go Factors of a Number
  • Go Perfect Number
  • Go Prime Number
  • Go Reverse a Number
  • Go Simple Interest
  • Go Square of a Number
  • Go Square root of a Number
  • Go Sum of Digits in a Number
  • Go Sum & Avg of Natural Nums
  • Go Sum of Even Numbers
  • Go Sum of Odd Numbers
  • Go Sum of Even and Odd
  • GoSwap Two Numbers
  • Go Sphere Vol & Surface Area
  • Go Cylinder Vol & Surface Area
  • Go Cuboid Vol & Surface Area
  • Go Cube Volume & Surface Area
  • Go Cone Volume & Surface Area
  • Go Arithmetic Operation on Array
  • Go Count Duplicates in Array
  • Go Even Numbers in Array
  • Go Largest Array Item
  • Go Largest & Smallest in Array
  • Go Negative Numbers in Array
  • Go Odd Numbers in an Array
  • Go Positive Numbers in Array
  • Go Pos & Neg in Separate Array
  • Go Reverse an Array
  • Go Search for Array Items
  • Go Smallest Array Item
  • Go Character is a Digit
  • Go Convert Character to Lower
  • Go Convert Character to Upper
  • Go ASCII Value of Character
  • Go ASCII Values of All Characters
  • Go Character is a Lowercase
  • Go Character is an Alphabet
  • Go Character is an Uppercase
  • Go Char Vowel or Consonant
  • Go Print Alphabets from a to z
  • Go Print Alphabets from A to Z
  • Go Matrix Arithmetic Operations
  • Go Program to Add Two Arrays
  • Go Matrix Multiplication Program
  • Go Program to Add Two Matrices
  • Go Identity Matrix
  • Go Sparse Matrix
  • Go Array Multiplication Program
  • Go Program to Print Array Items
  • Go Program to Print Matrix Items
  • Go Matrix Determinant
  • Go Sum of Each Matrix Column
  • Go Sum of Each Matrix Row
  • Go Sum of Matrix Diagonal
  • Go Sum of Matrix Lower Triangle
  • Go Sum of Matrix Upper Triangle
  • Go Print Matrix Upper Triangle
  • Go Print Matrix Lower Triangle
  • Go Sum of Matrix Each Row & Col
  • Go Sum of Matrix Opp Diagonal
  • Go Interchange Matrix Diagonals
  • Go Scalar Matrix Multiplication
  • Go Odd Index Position Array Item
  • Go Even Index Position Array Item
  • Go Symmetric Matrix
  • Go Two Matrixes are Equal
  • Go Count Positive & Neg in Array
  • Go Count Even and Odd in Array
  • Go Transpose a Matrix

Copyright © 2021ยท All Rights Reserved by Suresh.
About | Contact | Privacy Policy