How to to separate the negative number and positive number from array of 10 numbers in Golang 1.20.

Channel:
Subscribers:
5,740
Published on ● Video Link: https://www.youtube.com/watch?v=l7DWjBK4otU



Category:
Tutorial
Duration: 2:31
0 views
0


In this video we are going to generate source code for the GoLang Program, How to to separate the negative number and positive number from array of 10 numbers containing positive and negative numbers and store it in two different arrays using chatGPT.
After generating we will verify and run the generated code.

IMPORTANT NOTE on SOURCE CODE: only source code that are acceptable by the youtube description box will be displayed.
In this program I have replaced the angled brackets with > or <. once you copy the source code to editor replace it with greater than or less than symbols respectively.



##############SOURCE CODE#########################

package main

import "fmt"

func main() {
// Define an array of integers
arr := [...]int{2, -4, 5, -6, 7, -8, 9, -10, 11, -12}

// Initialize two arrays for positive and negative numbers
positive := []int{}
negative := []int{}

// Separate positive and negative numbers from the original array
for _, num := range arr {
if num >= 0 {
positive = append(positive, num)
} else {
negative = append(negative, num)
}
}

// Print the positive and negative arrays
fmt.Printf("Positive numbers: %v\n", positive)
fmt.Printf("Negative numbers: %v\n", negative)
}

##############END OF SOURCE CODE#########################

Below is the explanation for the program:

In this program, we define an array of integers using the ... syntax, which allows Go to infer the length of the array from the number of elements.

We then initialize two empty arrays positive and negative to store the positive and negative numbers, respectively.

We use a for loop to iterate over each element in the array. For each element, we check whether it is greater than or equal to 0. If it is, the number is positive or zero, so we append it to the positive array. Otherwise, the number is negative, so we append it to the negative array.

Finally, we print the positive and negative arrays using the fmt.Printf function and the %v verb to print the arrays.


#go #goprogramming #golang #golangtutorial #golanguage







Tags:
how to separate positive and negative numbers from an array in golang
go program to separate positive and negative numbers from an array and store in one array
arrays in go
go Programming Tutorial
go by example
go Language
go projects
go programming projects
go tutorial
golang tutorial
golang projects
go programming course
learn go
how to run go program
golang videos
golang with chatgpt
coding questions
coding interview questions