Golang Program To Display Upper Triangular Matrix


In this article, we will write a go language program to print the upper triangular matrix of any given matrix.

Introduction − A square matrix is called an upper triangular matrix if it has zero values below the principal diagonal. A matrix is displayed in its upper triangular form only if it is square. i.e. it must have the same number of rows and columns. The order of a typical upper triangular matrix is N X N.

Method 1: Display Upper Triangular Matrix using For Loops

In this method, we will write a go language program to display upper triangular matrix using for loops in the main() function.

Algorithm

Step 1 − Import the fmt package.

Step 2 − start the main() function.

Step 3 − Initialize variables of type int and a matrix of size 3 X 3.

Step 4 − Now, display the matrix on the screen using fmt.Println() function.

Step 5 − To display the matrix in its upper triangular form use the combination of for loop and if conditionals. The for loop help us to iterate over the array.

Step 6 − For every iteration if number of columns are greater than rows print the respective element of the matrix otherwise print zero in its place.

Step 7 − Repeat the process until every element of the matrix is iterated upon and print respective values.

Example

Golang program to display upper triangular matrix using for loops.

package main
import "fmt"
func main() {
   
   // initializing variables
   var i, j int
   matrixA := [3][3]int{
      {3, 1, 2},
      {4, 5, 6},
      {8, 9, 10},
   }

   // printing matrices on the screen
   fmt.Println("The given matrix is:")
   for i = 0; i < 3; i++ {
      for j = 0; j < 3; j++ {
         fmt.Print(matrixA[i][j], "\t")
      }
      fmt.Println()
   }
   
   // printing a new line
   fmt.Println()

   // getting upper triangle
   fmt.Println("The upper triangular matrix obtained from the matrix is:")
   for i = 0; i < 3; i++ {
      fmt.Println()
      for j = 0; j < 3; j++ {
         if j >= i {
            fmt.Print(matrixA[i][j], "\t")
         } else {
            fmt.Print("0 ", "\t")
         }
      }
   }
   fmt.Println()
}

Output

The given matrix is:
3    1    2
4    5    6
8    9   10

The upper triangular matrix obtained from the matrix is:
3    1    2
0    5    6
0    0   10

Method 2: Print the Upper Triangle Matrix using a User-defined Function

In this method, we will write a go language program to print the upper triangle matrix using an external user-defined function. The function will accept a matrix as an argument and will convert and print the upper triangular matrix of order N X N.

Algorithm

Step 1 − Import the fmt package.

Step 2 − Create a function to find the upper triangular matrix called upperTriangle(). This function accepts the matrix as an argument and returns the final matrix as result.

Step 3 − To display the matrix in its upper triangular form use the combination of for loop and if conditionals. The for loop help us to iterate over the array.

Step 4 − For every iteration, if the number of columns is greater than the rows print the respective element of the matrix otherwise print zero in its place.

Step 5 − Once all the iterations are complete return the result.

Step 6 − Now, start the main() function. Initialize a matrix of integer data types and store values to them. Further, print this matrix on the screen.

Step 7 − Call the upperTriangle() function by passing the two matrices as arguments to the function and store the result.

Step 8 − Print the final result obtained on the screen using fmt.Println() function.

Example

Golang program to print upper triangle matrix using external function.

package main
import "fmt"

// creating the upper triangular matrix function
func upperTriangle(matrixA [3][3]int) [3][3]int {
   var result [3][3]int
   for i := 0; i < 3; i++ {
      for j := 0; j < 3; j++ {
         if j >= i {
            result[i][j] = matrixA[i][j]
         } else {
            result[i][j] = 0
         }
      }
   }
   return result
}
func main() {

   // initializing variables
   var i, j int
   matrixA := [3][3]int{
      {3, 1, 2},
      {4, 5, 6},
      {8, 9, 10},
   }

   // printing matrices on the screen
   fmt.Println("The given matrix is:")
   for i = 0; i < 3; i++ {
      for j = 0; j < 3; j++ {
         fmt.Print(matrixA[i][j], "\t")
      }
      fmt.Println()
   }
   fmt.Println()

   // calling the upperTriangle() function
   result := upperTriangle(matrixA)
   fmt.Println("The upper triangular matrix obtained from the above matrix is:")
   for i = 0; i < 3; i++ {
      for j = 0; j < 3; j++ {
         fmt.Print(result[i][j], "\t")
      }
      fmt.Println()
   }
   fmt.Println()
}

Output

The given matrix is:
3    1    2
4    5    6
8    9   10

The upper triangular matrix obtained from the above matrix is:
3    1    2
0    5    6
0    0   10

Conclusion

We have successfully compiled and executed a go language program to print the upper triangle matrix along with an example. To achieve this we have used two programs one to display the upper triangular matrix using for loops in the main() function and in the other one we have used a user-defined function to achieve the result.

Updated on: 06-Jan-2023

209 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements