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.
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.
Program/Source Code:
The source code to add two matrices is given below. The given program is compiled and executed successfully.
Output:
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.
In the above code, we read elements for matrix1 and matrix2 from the user.
In the above code, we added two matrices and printed the result on the console screen.
need an explanation for this answer? contact us directly to get an explanation for this answer