Q:

Swift program to read a float number from the user

belongs to collection: Swift Basic Programs

0

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.

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

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 Int() function wi... >>
<< Swift program to read an integer number from the u...