# Swift Program to Print Diagonal Matrix Pattern

This tutorial will discuss how to write swift program to print Diagonal Matrix pattern.

Diagonal matrix are those matrix in which all the elements are 0 except the principal diagonal.Or we can say that in a diagonal matrix off diagonal blocks are 0 and main diagonal blocks are square matrices.

To create a diagonal matrix pattern we can use any of the following method −

Using nested for loop

Using stride Function

Below is a demonstration of the same −

Input

Suppose our given input is −

Num = 3


Output

The desired output would be −

1 0 0
0 2 0
0 0 3


## METHOD 1- USING NESTED FOR LOOP

We can create a diagonal matrix or any other pattern using nested for loops. Here each for loop handle different tasks such as outermost for loop is used for new rows, and nested for loop is used to print diagonal matrix.

### Example

The following program shows how to print diagonal matrix pattern using nested for loop.

import Foundation
import Glibc

// Size of the pattern
let num = 4

// Handling the rows of the matrix
for m in 1...num {
// Creating diagonal matrix
for n in 1...num {
if m == n {
print(n, terminator : " ")
}
else {
print(0, terminator : " ")
}
}
print(" ")
}


### Output

1 0 0 0
0 2 0 0
0 0 3 0
0 0 0 4


Here, in the above code, we use nested for loops to print diagonal matrix. The outer most for loop(starts from 1 to 4) is use to handle the total number of rows are going to print and each row is start with new line. Now the nested for loop(starts from 1 to num) is used to print the diagonal matrix. Here if the m == n, then print number. Otherwise print 0.

## METHOD 2- USING STRIDE FUNCTION

Swift provide an in-built function named stride(). The stride() function is used to move from one value to another with increment or decrement. Or we can say stride() function return a sequence from the starting value but not include end value and each value in the given sequence is steps by the given amount.

### Syntax

Following is the syntax −

stride(from:startValue, to: endValue, by:count)


Here,

from − Represent the starting value to use for the given sequence.

to − Represent the end value to limit the given sequence

by − Represent the amount to step by with each iteration, here positive value represents upward iteration or increment and negative value represent the downward iteration or decrement.

### Example

The following program shows how to print diagonal matrix pattern using stride() function.

import Foundation
import Glibc

// Size of the pattern
let num = 8

// Handle the length of the matrix
for m in stride(from: 1, to: num, by: 1) {
// Printing diagonal matrix pattern
// Using stride() function
for n in stride(from: 1, to: num, by: 1) {
if m == n {
print(n, terminator : " ")
}
else {
print(0, terminator : " ")
}
}
print("")
}


### Output

1 0 0 0 0 0 0
0 2 0 0 0 0 0
0 0 3 0 0 0 0
0 0 0 4 0 0 0
0 0 0 0 5 0 0
0 0 0 0 0 6 0
0 0 0 0 0 0 7


Here in the above code, we uses nested for loops along with stride() function. The outermost for loop is used to handle the total number of rows are going to print and each row starts with a new line. The nested for loop is used to print diagonal matrix using stride() function −

for n in stride(from: 1, to: num, by: 1) {
if m == n {
print(n, terminator : " ")
}
else {
print(0, terminator : " ")
}
}


Here the iteration starts from 1 to num and each iteration increase by one. To print the diagonal matrix, we check m == n. If the condition is true, then print n. Otherwise print 0.