Skip to main content

Swift programs to print different star patterns

Hello All. Thank you for joining.

As we all know, everyone in the software domain goes through the tasks of printing patterns in an interview process. However, sometimes it seems difficult for a beginner to perform these tasks. So here are some programs in Swift to print different patterns which might help. Please feel free to correct me if I am wrong.

Pattern 1: Given a number int, write a program to print a below shape with n rows.











Solution:

func upsideDown(_ int : Int)
{
    for i in 0...int-1
    {
        print(String.init(repeating: "*", count: int-i))
    }
}

upsideDown(6)



Pattern 2: Given a number int, write a program to print a below shape with n rows.












Solution:
int = number of rows
i = row number from 1 to int
So count number of spaces and stars in each row and accordingly find the expression
For this pattern expression is:
Spaces count = int - i
Star Count = 2(i) - 1


func starprintTraingle(_ int : Int)
{
 for i in 1...int
 {
   print(String.init(repeating: " ", count: int-i) + String.init(repeating: "*", count: 2*i - 1))
 }
}
starprintTraingle(8)



Pattern 3: Given a number int, write a program to print a below shape with n rows.











Solution: 
int = number of rows
i = row number from 1 to int
So count number of spaces and stars in each row and accordingly find the expression
For this pattern expression is:
Spaces count = int - i
Star Count = 2(i) - 1

func starprintupsideDownTraingle(_ int : Int)
{
    var i = int
    while i >= 1
    {
      print(String.init(repeating: " ", count: int-i) + String.init(repeating: "*", count: 2*i - 1))
      i = i-1
    }
}
starprintupsideDownTraingle(8)


Pattern 4: 
Given a number int, write a program to print a below shape with n rows.













Solution:
int = number of rows
i = row number from 1 to int
So count number of spaces and stars in each row and accordingly find the expression
For this pattern expression is:
Spaces count = int - i
Star Count = i

func starprintRightTraingle(_ int : Int)
{
  for i in 1...int
  {
     print(String.init(repeating: " ", count: int-i)+String.init(repeating: "*", count: i))
  }
}
starprintRightTraingle(6)



Pattern 5: Given a number n, write a program to print a diamond shape with n rows.


  
n=9 Odd number
   
n=8 Even number













Solution:
n = number of rows
i = row number from 1 to n
So count number of spaces and stars in each row and accordingly find the expression
For this pattern expression are according to odd and even input. 


func logDiamond(_ n: Int)
{
    var i = 1
    while i <= n {
    if n%2 == 0 // n- even
    {
        if i <= n/2
        {
            let str = String.init(repeating: "*", count:(i-1)*2+1)
            let space = String.init(repeating: " ", count:((n/2)-i))
            print(space+str)
        }
    }
    else  //n- odd
    {
        if i < n/2+1
        {
            let str = String.init(repeating: "*", count:(i-1)*2+1)
            let space = String.init(repeating: " ", count:((n/2)-i+1))
            print(space+str)
        }
     }
    if i > n/2
        {
            let str = String.init(repeating: "*", count:(n-i)*2+1)
            let space = String.init(repeating: " ", count:(i-((n/2)+1)))
            print(space+str)
    }
      i+=1
   }
}
logDiamond(8)
logDiamond(9)



Pattern 6: 
Given a number int, write a program to print a below shape with n rows.













Solution:
n = number of stars in square length and breadth
i = row number from 1 to int

func starprintSquare(_ int : Int)
{
    for i in 1...int
    {
        if i == 1 || i == int
        {
           print(String.init(repeating: "*", count: int))
        }
        else
        {
          print(String.init(repeating: "*", count: 1) + String.init(repeating: " ", count: int-2) + String.init(repeating: "*", count: 1))
        }
    }
}
starprintSquare(7)

Comments

Post a Comment

Popular posts from this blog

How to open a particular View Controller when the user taps on the push notification received?

Thank you for joining. Lets start First of all we will go through the Push notification flow  Push Notification Flow Here, I am considering that everyone has gone through APNs(Apple Push Notification service). If not then first go through this link . How the push notification flow takes place? Above image will help you understand the flow easily which is as follows: Your app registers for Push notification service. The device in which app is running will request for device token from APNs server along with device certificate. APNs will validate the certificates of application and generate a app-specific device token. APNS will send the device token to the application. This device token is then send to your/FCM server which will be providing notification. So whenever any interesting thing happens, your/FCM server sends push notification request along with device token to APNs. APNs decrypts the token to ensure the validity of the request and to determine the target device an

Keyboard handling - Key Point in iOS App Development

Hello and Thank you for joining. Today, let us go through a very significant topic which is Keyboard Handling . All iOS Developer meets this challenge once while making an app including me. So lets see how can we overcome this challenge. In this blog, we will understand the method to handle the keyboard's show  and hide  events and accordingly handle the View Controllers. We will be working on an example to understand the procedure to handle Keyboard Events . But before that let's go through some significant terms. The first thing you should know is the UIWindow- “The backdrop for your app’s user interface and the object that dispatches events to your views.” UIWindow is the one on which we display our app content. However, there are some more tasks which object of UIWindow handles. Those are as follows- Setting the z-axis level of your window, which affects the visibility of the window relative to other windows. Showing windows and making