Swift program to demonstrate the closed range operator

Here, we are going to demonstrate the closed range operator in Swift programming language.
Submitted by Nidhi, on June 01, 2021

Problem Solution:

Here, we will print the table of 5 using a closed range operator. The closed range operator (X...Y) specifies the range of numbers from X to Y.

Program/Source Code:

The source code to demonstrate the closed range operator is given below. The given program is compiled and executed successfully.

// Swift program to demonstrate the
// closed range operator

var num:Int=5;
var res:Int=0;

print("Table:")
for val in 1...10 {
    res = num*val;
    print("\(num)X\(val) = \(res)")
}

Output:

Table:
5X1 = 5
5X2 = 10
5X3 = 15
5X4 = 20
5X5 = 25
5X6 = 30
5X7 = 35
5X8 = 40
5X9 = 45
5X10 = 50

...Program finished with exit code 0
Press ENTER to exit console.

Explanation:

In the above program, we imported a package Swift to use the print() function using the below statement,

import Swift;

Here, we created two integer variables num, res, that are initialized 5,0 respectively. Then we iterated the numbers from 1 to 10 using the closed range operator in the for loop and printed the table of number 5 on the console screen.

Swift Basic Programs »



Related Programs




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.