Menu Close

Go – Program to print matrix in go

Here, we will write a program to print matrix in go. Given below, you can find algorithm and program.

Here, we are going to learn below things:

  • Take number of rows and columns as input
  • Apply nested for loop to take matrix element as input
  • Print the matrix element

Program to print matrix:

Input: row = 2, column = 3
[
1, 2, 3
4, 5, 6
]

Ouput:
[
1, 2, 3
4, 5, 6
]

Code:

package main

import (

 "fmt"

)

func main() {

  var row, col int

  var mat [10][10]int

  fmt.Print("Enter no of rows: ")

  fmt.Scanln(&row)

  fmt.Print("Enter no of column: ")

  fmt.Scanln(&col)


  fmt.Println("\nEnter matrix elements: ")

  for i := 0; i < row; i++ {

    for j := 0; j < col; j++ {

      fmt.Scanf("%d ", &mat[i][j])

    }

  }

  fmt.Println("\nMatrix is: \n")

  for i := 0; i < row; i++ {

    for j := 0; j < col; j++ {

      fmt.Printf("%d ", mat[i][j])

    }
    fmt.Println("\n")

  }

}

Output:

Enter no of rows: 2
Enter no of column: 2

Enter matrix elements:
1
2
3
4

Matrix is:
1 2
3 4

To learn more about golang. Please follow given below link.

https://www.techieindoor.com/go-lang/

References:

https://golang.org/doc/
https://golang.org/pkg/

Posted in golang, golang program

Leave a Reply

Your email address will not be published.