
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print Left Diagonal Matrix in Golang
In this article, we will see how to print the left diagonal matrix with the help of suitable examples. A matrix is a 2-d array. Here in the examples, we will use a nested for loop which will iterate through the rows and columns of the matrix to print the left diagonal matrix. The output will be printed on the screen using fmt.println() function which is a print statement in Golang.
Algorithm
Step 1 ? Create a package main and declare fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and output.
Step 2 ? Create a function main and in that function create a variable n which refers to the size of the matrix.
Step 3 ? Print the size of the matrix on the console.
Step 4 ? Create a nested loop and run it
Step 5 ? Inside the loop check a condition that if i is equals to j print 1 on the console and if its not equal to j print 0 on the console.
Step 6 ? Here the position of 1 is used to represent the left diagonal matrix and other non-diagonal elements are filled with 0.
Step 7 ? The square matrix with 0's and 1's is printed on the console using fmt.Println() function where ln refers to new line.
Using of Nested For Loop
In this example, we will look at how to print the left diagonal matrix using nested for loop. The two variables will be used to iterate through the inner and outer loops. Let's understand the example with the help of the algorithm and the code.
Example
package main import "fmt" // create function main to execute the program func main() { var n int = 3 //size of matrix fmt.Println("The size of the matrix is:", n) //print size of matrix fmt.Println("The left diagonal matrix presented here is:") for i := 0; i < n; i++ { //use of nested for loop for j := 0; j < n; j++ { if i == j { fmt.Print("1 ") // if the condition satisfies print 1 } else { fmt.Print("0 ") // else print 0 } } fmt.Println() // used for a new line } }
Output
The size of the matrix is: 3 The left diagonal matrix presented here is: 1 0 0 0 1 0 0 0 1
Using Nested For Loop with i<=j
In this example, we will grasp how to print the left diagonal matrix using a different condition in nested for loop than we used in last example. The square matrix will be printed on the console at the end. Let's see through the code and algorithm how to execute this.
Example
package main import "fmt" // create main function to execute the program func main() { size := 3 // size of square matrix fmt.Println("The size of matrix going to printed here is:", size) // print size fmt.Println("The left diagonal matrix is:") for i := 0; i < size; i++ { for j := 0; j < size; j++ { if i <= j { fmt.Print("1 ") //print 1 if condition satisfies } else { fmt.Print("0 ") //else print 0 } } fmt.Println() // used for new line } }
Output
The size of matrix going to printed here is: 3 The left diagonal matrix is: 1 1 1 0 1 1 0 0 1
Conclusion
We executed the program of printing the left diagonal matrix using two examples. In both the examples given above we have used nested for loop with different set of conditions. The output printed on the console is a square matrix representing left diagonal matrix. Hence, program executed successfully.