Swift Program – Print Numbers From 1 to n

Print Numbers From 1 to n

To print numbers from 1 to n, use for loop with range. The range would be 1...n. And for each number in the range, we will print the number.

In the following program, we will take a value of 10 for n, and print numbers from 1 to n.

main.swift

import Foundation

var n = 10

for i in 1...n {
    print(i)
}

Program Output

1
2
3
4
5
6
7
8
9
10

Summary

Summarising this tutorial, to iterate over a range of numbers, use for loop with range.