 GO Program to print even numbers in an array

How to print even numbers in an array

Here we are explaining how to print even 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 even 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 even 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 even numbers by iterating through the loop. If (Arr[i]%2 == 0) then it is an even 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 even number in a for loop as  if (Arr[i] %2==0)

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

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

GO Source Code

package main
import "fmt"

func main() {
var size, i int
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 even 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 even numbers in Arr = 2 6 8 4 12
VIEW ALL
VIEW ALL
OtherTutorials
VIEW ALL 