Go Program to Print Odd Numbers in an Array

Write a Go program to Print Odd Numbers in an Array using For loop. Here, we used for loop (for i = 0; i < odsize; i++) to iterate array items. Within the loop, the if condition (if oddArr[i]%2 != 0) checks whether the item is not divisible by two. If True, it is an odd number, so print that number.

package main

import "fmt"

func main() {
    var odsize, i int

    fmt.Print("Enter the Odd Array Size = ")
    fmt.Scan(&odsize)

    oddArr := make([]int, odsize)

    fmt.Print("Enter the Odd Array Items  = ")
    for i = 0; i < odsize; i++ {
        fmt.Scan(&oddArr[i])
    }
    fmt.Print("The List of Odd Numbers in oddArr = ")
    for i = 0; i < odsize; i++ {
        if oddArr[i]%2 != 0 {
            fmt.Print(oddArr[i], " ")
        }
    }
    fmt.Println()
}
Enter the Odd Array Size = 5
Enter the Odd Array Items  = 1 22 9 4 33
The List of Odd Numbers in oddArr = 1 9 33 

Go Program to Print Odd Numbers in an Array using For Loop range

package main

import "fmt"

func main() {
    var odsize int

    fmt.Print("Enter the Odd Array Size = ")
    fmt.Scan(&odsize)

    oddArr := make([]int, odsize)

    fmt.Print("Enter the Odd Array Items  = ")
    for i := 0; i < odsize; i++ {
        fmt.Scan(&oddArr[i])
    }
    fmt.Print("The List of Odd Numbers in oddArr = ")
    for _, od := range oddArr {
        if od%2 != 0 {
            fmt.Print(od, " ")
        }
    }
    fmt.Println()
}
Go program to Print Odd Numbers in an Array

In this Golang program, we created a function (printOddNums(oddArr []int) ) to print odd numbers in a given array.

package main

import "fmt"

func printOddNums(oddArr []int) {
    fmt.Print("The List of Odd Numbers in oddArr = ")
    for _, od := range oddArr {
        if od%2 != 0 {
            fmt.Print(od, " ")
        }
    }
}

func main() {
    var odsize int

    fmt.Print("Enter the Odd Array Size = ")
    fmt.Scan(&odsize)

    oddArr := make([]int, odsize)

    fmt.Print("Enter the Odd Array Items  = ")
    for i := 0; i < odsize; i++ {
        fmt.Scan(&oddArr[i])
    }
    printOddNums(oddArr)
    fmt.Println()
}
Enter the Odd Array Size = 10
Enter the Odd Array Items  = 1 0 9 6 11 43 98 33 -22 -25
The List of Odd Numbers in oddArr = 1 9 11 43 33 -25