GO Program to print odd numbers in an array

How to print odd numbers in an array

Here we are explaining how to print odd numbers in an array in the Go program. We can use the built-in function fmt.println() to print anything and fmt.scanln() for reading the values. This function is defined under the fmt package and it helps to write standard output. In order to use these functions, we need to import the “fmt” package.

 

How to print odd numbers in an array using GO Program

We are using fmt.println() function for printing the string to the output screen. Here we are showing how to print odd numbers in an array in the Go language. Here variable Arr holds the array elements. Other variables size, i used as the size of the array and index for the loop. Use for loop to find the odd numbers by iterating through the loop. If (Arr[i]%2 != 0) then it is an odd number and print that number. Given below are the steps which are used in the Go program. 

ALGORITHM

STEP 1: Import the package fmt

STEP 2: Start function main()

STEP 3: Declare the variable size,i

STEP 4: Read the array Arr[] using for loop

STEP 5: Search for the odd number in a for loop as  if (Arr[i] %2!=0)

STEP 6: If true it is an odd number otherwise continue the loop

STEP 7:Print the odd numbers using fmt.Println()

 

GO Source Code

                                              fmt.Print("Enter the array size = ")
    fmt.Scan(&size;)
    Arr := make([]int, size)
    fmt.Print("Enter the array items  = ")
    for i = 0; i < size; i++ {
        fmt.Scan(&Arr;[i])
    }
    fmt.Print("\nThe list of odd numbers in Arr = ")
    for i = 0; i < size; i++ {
        if Arr[i]%2 != 0 {
            fmt.Print(Arr[i], " ")
        }
    }
    fmt.Println()
}
                                      

OUTPUT

Enter the array size = 10
Enter the array items  = 2 5 6 3 8 4 7 9 12 15

The list of odd numbers in Arr = 5 3 7 9 15