Skip to main content
Golang

Print Odd-Even Series Using Two Goroutines in Go

Prerna Sharma

Printing a series of odd-even up to a certain number is one of the very important questions of Golang Interviews. And this question can be asked in multiple ways. In this article, we will be looking at two different ways of printing the series of odd-even. to achieve the same.

Let's say we want to print a series of odd-even numbers up to a certain number n. To achieve this we will be using goroutines and the sync package.

For simplicity, let's assume n = 20.

Create a oddeven.go file in your directory.

package main

import (
	"fmt"
	"sync"
)

/*
printOdd prints odd numbers up to n.
It uses a sync.WaitGroup to signal when it's done.
*/
func printOdd(n int, wg *sync.WaitGroup) {
	defer wg.Done()
	for i := 1; i <= n; i += 2 {
		fmt.Printf("%d ", i)
	}
}

/*
printEven prints even numbers up to n.
It uses a sync.WaitGroup to signal when it's done.
*/
func printEven(n int, wg *sync.WaitGroup) {
	defer wg.Done()
	for i := 2; i <= n; i += 2 {
		fmt.Printf("%d ", i)
	}
}

/*
PrintOddEven prints odd and even numbers up to n concurrently.
It uses two goroutines and a sync.WaitGroup for synchronization.
*/
func PrintOddEven(n int) {
	// Create a WaitGroup to wait for the completion of goroutines.
	var wg sync.WaitGroup
	// Add 2 to the WaitGroup counter since there are two goroutines.
	wg.Add(2)

	// Launch a goroutine to print odd numbers.
	go printOdd(n, &wg)
	// Launch another goroutine to print even numbers.
	go printEven(n, &wg)

	// Wait for both goroutines to complete.
	wg.Wait()
}

To test this code, let's create a main.go file.

package main

func main() {
	PrintOddEven(20)
}

Now to run the program fire this command go run . in your terminal.

Output

2 4 6 8 10 12 14 16 18 20 1 3 5 7 9 11 13 15 17 19

After running the program, you might get different results. But this is how the above code will print the series of odd-even numbers up to 20.

We will create two channels, evenChannel and oddChannel, and spawn two goroutines to generate even and odd numbers respectively. The PrintOddEvenWithChannel function then reads from these channels using range loops and prints the received numbers.

Let's look at the code.

Create a oddevenchannel.go file in your directory.

package main

import "fmt"

/*
generateEvenNumbers generates even numbers up to n and sends them to the given evenChannel.
It closes the channel after sending all even numbers.
*/
func generateEvenNumbers(n int, evenChannel chan<- int) {
	for i := 2; i <= n; i += 2 {
		evenChannel <- i
	}
	close(evenChannel)
}

/*
generateOddNumbers generates odd numbers up to n and sends them to the given oddChannel.
It closes the channel after sending all odd numbers.
*/
func generateOddNumbers(n int, oddChannel chan<- int) {
	for i := 1; i <= n; i += 2 {
		oddChannel <- i
	}
	close(oddChannel)
}

/*
PrintOddEvenWithChannel generates and prints odd and even numbers up to n using channels.
It launches two goroutines to generate even and odd numbers concurrently.
Then, it prints the numbers by receiving them from the channels.
*/
func PrintOddEvenWithChannel(n int) {
	// Create channels for even and odd numbers.
	evenChannel := make(chan int)
	oddChannel := make(chan int)

	// Launch goroutines to generate even and odd numbers concurrently.
	go generateEvenNumbers(n, evenChannel)
	go generateOddNumbers(n, oddChannel)

	// Receive and print even numbers until the evenChannel is closed.
	for evenNumber := range evenChannel {
		fmt.Printf("%d ", evenNumber)
	}

	// Receive and print odd numbers until the oddChannel is closed.
	for oddNumber := range oddChannel {
		fmt.Printf("%d ", oddNumber)
	}
}

To test this code, let's create a main.go file.

package main

func main() {
	PrintOddEvenWithChannel(20)
}

To run this code, fire go run . in your terminal.

Output

2 4 6 8 10 12 14 16 18 20 1 3 5 7 9 11 13 15 17 19

After running the program, you might get different results. But this is how the above code will print the series of odd-even numbers up to 20.

Conclusion

In this article, we have looked at how we can print the series of odd-even numbers using the concept of Goroutines and Channels in Go.

If you want to read the advanced version of this problem, follow the link.

Connect with us on Discord in case you are stuck or have any questions.