Swift program to read a float number from the user

Here, we are going to learn how to read a float 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 a float number using the Float() function and print the number on the console screen.

Program/Source Code:

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

// Swift program to read a float number
// from the user

import Swift;

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

Output:

Enter Number:
3.14
Entered number is:  3.14

...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 a float number using the Float() 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.