Q:

Swift program to demonstrate the closed range operator

belongs to collection: Swift Basic Programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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 numres, 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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Swift Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Swift program to demonstrate the Half-Open range o... >>
<< Swift program to demonstrate the logical NOT (!) o...