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


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]

Updated on: 18-Mar-2021

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements