How to Read The Number From Standard Input in Swift Program?


This tutorial will discuss how to write a swift program to read the number from standard input. Reading a number from standard input in Swift is very easy with the help of readLine(), Int(), and Float() functions.

readLine() function − This function returns a string of characters which is read from the standard input at the end of the current line. If the control is already reached to the EOF when the readLine() function is called then this method will return nil.

Syntax

Following is the syntax of Swift readLine() function −

readLine(strippingNewline: true)

Or

readLine()

Int() function − This function is used to convert string into a signed integer value type. The size of the integer depends upon the platform means if the platform is 32 bit then the size is Int32.

Syntax

Following is the syntax of Swift Int() function −

Int()

Float() function − This function is used to convert string into a single-precision, floating point value type.

Syntax

Following is the syntax of Swift Float() function −

Float()

Algorithm to take input from User

  • Step 1 − Define a variable

  • Step 2 − Use readline() syntax to store the input value in above variable

  • Step 3 − Print that variable

  • Step 4 − Perform various operations with that variable

  • Step 5 − Print output

Example

Reading integer type number from the Standard Input

The following Swift program will show how to read an integer type number from the standard Input using readLine() and Int() functions. Here first we read the radius of the circle as an input from the user using readLine() function then convert that string type input into integer type using Int() function. After that we find the area of the circle.

import Foundation
import Glibc
print("Please enter the radius(r) of the circle:")
if let readValue = readLine() {
   if let radius = Int(readValue) {
      let area = (22 * radius * radius)/7
      print("Hence the area of the circle:", area)
   }
}

Input

Please enter the radius(r) of the circle:
20

Output

Hence the area of the circle: 1257

In the above code, first we read the radius of the user(i.e., 20) using the readLine() function and assign it to a variable. The readLine() function reads data in string formate so we convert the radius into integer numb er using Int() function. After reading the radius we calculate the area of the circle using the following code

let area = (22 * radius * radius)/7

and display the area of the circle on the output screen that is 1257.

Example

Reading float type number from the Standard Input

The following Swift program will show how to read a float type number from the standard Input using readLine() and Float() functions. Here first we read the radius of the circle as an input from the user using readLine() function then convert that string type input into float type using Float() function. After that we find the circumference of the circle.

import Foundation
import Glibc
print("Please enter the radius(r) of the circle:")
if let readValue = readLine() {
   if let radius = Float(readValue) {
      let Circumference = 2 * 3.14 * radius
      print("Hence the Circumference is:", Circumference)
   }
}

Input

Please enter the radius(r) of the circle:
10

Output

Hence the Circumference is: 62.800003

In the above code, first we read the radius of the user(i.e. 10) using the readLine() function and assign it to a variable. The readLine() function reads data in string formate so we convert the radius into floating point number using Float() function. After reading the radius we calculate the circumference of the circle using the following code

let Circumference = 2 * 3.14 * radius

and display the circumference of the circle on the output screen that is 62.800003

Updated on: 28-Jul-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements