1

I have a C program like this that I want to translate to Swift:

int main(void)    
{   
    int i, j;

    for(i=1;i<=5;i++) 
    {
        printf("\n");
        for(j=1;j<=5-i;j++)
        {
            printf("_");
        }
        for(j=1;j<=i;j++)
        {
            printf("%d",j);
        }

        for(j=i-1;j>=1;j--)
        {
            printf("%d",j);
        }
    }
    printf("\n");
}

Output:

    1
   121
  12312
 1234123
123454321

I'm learning Swift and I wanted to make this pattern but I'm stuck with the conditions in the 3rd loop.

What should I be writing there?

Swift code:

import Foundation

var no = 1
for i in 1...5
{ 
    no = 1

    for j in 1..<(6-i)
    {
        print("_" , terminator : " ")

    }
    for k in 1...i
    {
        print("\(no)", terminator : " ")
        no+=1
    }
    for l in // 3rd loop , what would be the conditions ?

    }
    print("\n")
}
0

7 Answers 7

3

Swift 4, Xcode 9.1

var j: Int = 0
var pattern = String()
   for i in 1...5 {
       for _ in 1...6 - i {
           pattern += " "
       }
       for j in 1...i {
           pattern += "\(j)"
       }

       j = i - 1
       while j >= 1 {
           pattern += "\(j)"
           j -= 1
       }
        pattern += "\n"
   }
   print(pattern)

Output:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

check this in your Playground

var str: String = ""
for i in 1..<5 {
    print(i, terminator : " ", to: &str)
}
print(str)
str = ""

for i in 10...15
{
    print(i, terminator : " ", to: &str)
}
print(str)
str = ""

for i in (20..<25).reversed() {
    print(i, terminator: " ", to: &str)
}
print(str)
str = ""

for i in (30...35).reversed() {
    print(i, terminator: " ", to: &str)
}
print(str)
str = ""

it prints

1 2 3 4 
10 11 12 13 14 15 
24 23 22 21 20 
35 34 33 32 31 30 

Comments

0

use the stride function

for j in stride(from: i-1, through: 1, by: -1) {
    print("\(j)")
}

1 Comment

Thank You so much
0

In the fourth loop, a condition will be.

for l in 1.. < no - 1
{
    no -= 1
    print("\(no - 1)" , terminator : "")
}
print("\n")

Comments

0
var number = 1
var kN = 1
for i in 1...5{
    for j in stride(from: 5, to: i, by: -1){
        print(" ", separator: "", terminator: "")   
       }
    for k in stride(from: 1, through: kN, by: 1){

        print("\(number) ", separator: "", terminator: "")
        if number >= i || k > i{
            number  = number - 1
        }
        else{
            number = number + 1
        }

    }
    number = 1
    kN = kN + 2
    print("")
}

Comments

0

Swift 4.2, Xcode 10.1

var j = 0
var pattern = String()
for i in 0...6 
{
    for _ in 0...7 - i 
    {
        pattern += "  "
    }

    for j in 0...i 
    {
        pattern += " \(j)"
    }
    j = i - 1

    while j >= 0 
    {
        pattern += " \(j)"
        j -= 1
    }
    pattern += "\n"
}
print(pattern)


----------    **result**    ------------

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

Comments

0

Swift 5.0

    func numberMiddle(numberOfRow: Int){
    var no = 1
    for i in 1...numberOfRow {
        for _ in 1..<(numberOfRow-i) {
            print(" ", terminator: " ")
        }
        for j in 1...i {
            print("\(j)", terminator: " ")
            no = j
        }
        for _ in 1..<no {
            no -= 1
            print("\(no)", terminator: " ")
        }
        print(" ")
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.