×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang program to add two matrices

Here, we are going to learn how to add two matrices in Golang (Go Language)?
Submitted by Nidhi, on March 08, 2021 [Last updated : March 03, 2023]

How to add two matrices in Golang?

Problem Solution:

In this program, we will read elements for matrix1 and matrix2 from the user and then add both matrices and also print the matrix on the console screen.

Program/Source Code:

The source code to add two matrices is given below. The given program is compiled and executed successfully.

Golang code to add two matrices

// Golang program to add two matrices.

package main

import "fmt"

func main() {
	var matrix1 [2][2]int
	var matrix2 [2][2]int
	var matrix3 [2][2]int

	fmt.Printf("Enter matrix1 elements: \n")
	for i := 0; i < 2; i++ {
		for j := 0; j < 2; j++ {
			fmt.Printf("Elements: matrix1[%d][%d]: ", i, j)
			fmt.Scanf("%d", &matrix1[i][j])
		}
	}

	fmt.Printf("Enter matrix2 elements: \n")
	for i := 0; i < 2; i++ {
		for j := 0; j < 2; j++ {
			fmt.Printf("Elements: matrix2[%d][%d]: ", i, j)
			fmt.Scanf("%d", &matrix2[i][j])
		}
	}

	//Add both matrix1 and matrix2
	for i := 0; i < 2; i++ {
		for j := 0; j < 2; j++ {
			matrix3[i][j] = matrix1[i][j] + matrix2[i][j]
		}
	}

	fmt.Printf("Matrix1: \n")
	for i := 0; i < 2; i++ {
		for j := 0; j < 2; j++ {
			fmt.Printf("%d ", matrix1[i][j])
		}
		fmt.Printf("\n")
	}

	fmt.Printf("Matrix2: \n")
	for i := 0; i < 2; i++ {
		for j := 0; j < 2; j++ {
			fmt.Printf("%d ", matrix2[i][j])
		}
		fmt.Printf("\n")
	}

	fmt.Printf("Addition of two matrices: \n")
	for i := 0; i < 2; i++ {
		for j := 0; j < 2; j++ {
			fmt.Printf("%d ", matrix3[i][j])
		}
		fmt.Printf("\n")
	}
}

Output:

Enter matrix1 elements:
Elements: matrix1[0][0]: 11
Elements: matrix1[0][1]: 22
Elements: matrix1[1][0]: 33
Elements: matrix1[1][1]: 44
Enter matrix2 elements:
Elements: matrix2[0][0]: 55
Elements: matrix2[0][1]: 66
Elements: matrix2[1][0]: 77
Elements: matrix2[1][1]: 88
Matrix1:
11 22
33 44
Matrix2:
55 66
77 88
Addition of two matrices:
66 88
110 132

Explanation:

In the above program, we declare the package main. The main package is used to tell the Go language compiler that the package must be compiled and produced the executable file. Here, we imported the fmt package that includes the files of package fmt then we can use a function related to the fmt package.

In the main() function, we created two 2X2 matrices using a two-dimensional array.

fmt.Printf("Enter matrix1 elements: \n")
for i:=0;i<2;i++{
    for j:=0;j<2;j++{
        fmt.Printf("Elements: matrix1[%d][%d]: ",i,j)
        fmt.Scanf("%d",&matrix1[i][j])
    }
}
    
fmt.Printf("Enter matrix2 elements: \n")
for i:=0;i<2;i++{
    for j:=0;j<2;j++{
        fmt.Printf("Elements: matrix2[%d][%d]: ",i,j)
        fmt.Scanf("%d",&matrix2[i][j])
    }
}

In the above code, we read elements for matrix1 and matrix2 from the user.

//Add both matrix1 and matrix2
for i:=0;i<2;i++{
    for j:=0;j<2;j++{
        matrix3[i][j]=matrix1[i][j]+matrix2[i][j]
    }
}

In the above code, we added two matrices and printed the result on the console screen.

Golang Array Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.