Golang program to create an integer array that takes inputs from users.

Go ProgrammingServer Side ProgrammingProgramming

Example

Approach

  • Ask the user to enter the size of array.

  • Make an integer array of given size.

  • Ask the user to enter elements.

  • At the end, print the array.

Example

 Live Demo

package main
import (
   "fmt"
)
func main(){
   fmt.Printf("Enter size of your array: ")
   var size int
   fmt.Scanln(&size)
   var arr = make([]int, size)
   for i:=0; i<size; i++ {
      fmt.Printf("Enter %dth element: ", i)
      fmt.Scanf("%d", &arr[i])
   }
   fmt.Println("Your array is: ", arr)
}

Output

Enter size of your array: 6
Enter 0th element: 10
Enter 1th element: 20
Enter 2th element: 30
Enter 3th element: 40
Enter 4th element: 50
Enter 5th element: 60
Your array is: [10 20 30 40 50 60]
raja
Published on 18-Mar-2021 16:50:41
Advertisements