ADVERTISEMENT
ADVERTISEMENT

Golang program to print the right diagonal of the matrix

Here, we are going to learn how to print the right diagonal of the matrix in Golang (Go Language)?
Submitted by Nidhi, on March 08, 2021

Problem Solution:

In this program, we will read elements of the matrix from the user and then print the right diagonal of the matrix on the console screen.

Program/Source Code:

The source code to print the right diagonal of the matrix is given below. The given program is compiled and executed successfully.

// Golang program to print the right 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("Right diagonal of Matrix: \n")
	for i := 0; i < 3; i++ {
		for j := 0; j < 3; j++ {
			if i+j == 2 {
				fmt.Printf("%d ", matrix[i][j])
			} else {
				fmt.Printf(" ")
			}
		}
		fmt.Printf("\n")
	}
}

Output:

Enter matrix elements:
Elements: matrix[0][0]: 11 
Elements: matrix[0][1]: 22 
Elements: matrix[0][2]: 33 
Elements: matrix[1][0]: 44 
Elements: matrix[1][1]: 55 
Elements: matrix[1][2]: 66 
Elements: matrix[2][0]: 77 
Elements: matrix[2][1]: 88 
Elements: matrix[2][2]: 99 
Matrix:
11 22 33
44 55 66
77 88 99
Right diagonal of Matrix:
  33
 55
77

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.

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("Right diagonal of Matrix: \n")
for i:=0;i<3;i++{
    for j:=0;j<3;j++{
        if(i+j==2){
            fmt.Printf("%d ",matrix[i][j])
        }else{
            fmt.Printf(" ")
        }
    }
    fmt.Printf("\n")
}

In the above code, we printed the matrix and right diagonal elements on the console screen.

Golang Array Programs »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.