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:
Pattern 2: Given a number int, write a program to print a below shape with n rows.
Solution:
Solution:
Solution:
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)
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)
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)
Helpful
ReplyDeletegreat helpful!!
ReplyDeleteWonderful! Keep Going
ReplyDelete