Swift program to read an integer number from the user

Here, we are going to learn how to read an integer number from the user in Swift programming language?
Submitted by Nidhi, on June 01, 2021

Problem Solution:

Here, we will read a numeric string from the user using the readLine() function and then convert the string to an integer using Int() function and print the number on the console screen.

Program/Source Code:

The source code to read an integer number from the user is given below. The given program is compiled and executed successfully.

// Swift program to read an integer number 
// from the user

import Swift;

print("Enter Number:")
if let val = readLine(){
    if let num = Int(val)
    {
        print("Entered number is: ",num);
    }
}

Output:

Enter Number:
108
Entered number is:  108

...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 read a numeric string using the readLine() function. Then convert the string into integer using Int() function and print the result on the console screen.

Swift Basic Programs »



Related Programs



Comments and Discussions!

Load comments ↻





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