Swift Program To Print X Star Pattern


This tutorial will discuss how to write swift program to print X star pattern.Star pattern is a sequence of “*” which is used to develop different patterns or shapes like pyramid, rectangle, cross, etc. These star patterns are generally used to understand or practice the program flow controls, also they are good for logical thinking.

To create a X star pattern we can use any of the following methods

  • Using nested for loop
  • Using stride Function

Below is a demonstration of the same −

Input

Suppose our given input is −

Num = 5

Output

The desired output would be −

*       *
 *     * 
  *   *  
   * *   
    *    
   * *   
  *   *  
 *     * 
*       *

Method 1- Using nested for loop

We can create a X pattern of “*” 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, nested for loop is used for “*”, etc.

Example

The following program shows how to print X star pattern using nested for loop.

import Foundation
import Glibc
var num = 6
// Total number of row and column in the pattern
var n = num * 2 - 1
// Print rows
for i in 1...n{
   // Print "*" in X pattern
   for j in 1...n{
      // For first and second diagonal 
      if ((j == i) || (j == (n - i + 1))){
         print("*", terminator: "")
      }
      else{
         print("", terminator: " ")
      }
   } 
   // Print New line
   print("")
}

Output

*         *
 *       * 
  *     *  
   *   *   
    * *    
     *     
    * *    
   *   *   
  *     *  
 *       * 
*         *

Here, in the above code, we uses nested for loops to print X pattern of “*”. So first we determine the exact number of rows and column using: n = num * 2 - 1(so rows = 11 and columns = 11).Now the outer most for loop(starts from 1 to n) is use to handle the total number of rows are going to print and each row is start with new line. Nested for loop(starts from 1 to n) is used to handle white spaces and “*”. So to print the X cross pattern we use the following conditions, else print the white space.

if(i==j) is used to print first diagonal.

if(j == (n - i + 1)) is used to print second diagonal.

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 used 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 represent upward iteration or increment and negative value represent the downward iteration or decrement.

Example

The following program shows how to print X star pattern using stride() function.

import Foundation
import Glibc
var num = 3
// Total number of rows and columns in the pattern
var n = num * 2 + 1
// Print rows
for i in stride(from:2, to: n, by: 1){
   // Print "*" in X pattern
   for j in stride(from:1, to: n, by: 1){
      // For first and second diagonal 
      if ((j == i) || (j == (n - i + 1))){
         print("*", terminator: "")
      }
      else{
         print(terminator: " ")
      }
   }  
   // Add new line
   print("")
}

Output

 *   *
  * * 
   *  
  * * 
 *   *

Here in the above code, the total number of rows and columns are going to print in the X pattern is

N = num * 2 + 1

Now we use for loop with stride() function to handle the rows of the X pattern and it also add new line after each row. The stride() function starts from 2 to n and in each iteration the value of i increased by 1. In this for loop, we use nested for loop along with stride function to print the “*” and spaces. So the nested stride function started from 1 to n and each iteration the value of j is increased by 1. As we known that X pattern is made up of two diagonals so to print the first diagonal we use if(i == j) condition and to print second diagonal we uses if(j == (n - i + 1)) condition. If both the conditions are no satisfy then print white space.

Updated on: 13-Dec-2022

377 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements