The source code to print the left diagonal of the matrix is given below. The given program is compiled and executed successfully.
// Golang program to print the left diagonal of the matrix
package main
import "fmt"
func main() {
var matrix [3][3]int
fmt.Printf("Enter matrix elements: \n")
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
fmt.Printf("Elements: matrix[%d][%d]: ", i, j)
fmt.Scanf("%d", &matrix[i][j])
}
}
fmt.Printf("Matrix: \n")
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
fmt.Printf("%d ", matrix[i][j])
}
fmt.Printf("\n")
}
fmt.Printf("Left diagonal of Matrix: \n")
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
if i == j {
fmt.Printf("%d ", matrix[i][j])
} else {
fmt.Printf(" ")
}
}
fmt.Printf("\n")
}
}
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 3X3 matrix using a two-dimensional array.
fmt.Printf("Enter matrix elements: \n")
for i:=0;i<3;i++{
for j:=0;j<3;j++{
fmt.Printf("Elements: matrix[%d][%d]: ",i,j)
fmt.Scanf("%d",&matrix[i][j])
}
}
In the above code, we read matrix elements from the user.
fmt.Printf("Matrix: \n")
for i:=0;i<3;i++{
for j:=0;j<3;j++{
fmt.Printf("%d ",matrix[i][j])
}
fmt.Printf("\n")
}
fmt.Printf("Left diagonal of Matrix: \n")
for i:=0;i<3;i++{
for j:=0;j<3;j++{
if(i==j){
fmt.Printf("%d ",matrix[i][j])
}else{
fmt.Printf(" ")
}
}
fmt.Printf("\n")
}
In the above code, we printed the matrix and left diagonal elements on the console screen.
Program/Source Code:
The source code to print the left diagonal of the matrix 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 3X3 matrix using a two-dimensional array.
In the above code, we read matrix elements from the user.
In the above code, we printed the matrix and left diagonal elements on the console screen.
need an explanation for this answer? contact us directly to get an explanation for this answer