How to separate the even numbers and odd numbers from array of 10 numbers in Golang 1.20

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



Category:
Tutorial
Duration: 3:11
2 views
0


In this video we are going to generate source code for the GoLang Program, How to separate the even numbers and odd numbers from array of 10 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.



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

package main

import "fmt"

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

// Initialize two arrays for even and odd numbers
even := []int{}
odd := []int{}

// Separate even and odd numbers from the original array
for _, num := range arr {
if num%2 == 0 {
even = append(even, num)
} else {
odd = append(odd, num)
}
}

// Print the even and odd arrays
fmt.Printf("Even numbers: %v\n", even)
fmt.Printf("Odd numbers: %v\n", odd)
}

##############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 even and odd to store the even and odd numbers, respectively.

We use a for loop to iterate over each element in the array. For each element, we check whether it is even or odd by checking its remainder when divided by 2. If the remainder is 0, the number is even, so we append it to the even array. Otherwise, the number is odd, so we append it to the odd array.

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

#go #goprogramming #golang #golangtutorial #golanguage







Tags:
how to separate odd and even numbers from an array in golang
go program to separate odd and even 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