Swift Program to Multiply Two Floating-Point Numbers


This tutorial will discuss how to write a swift program to multiply two floating point numbers.

Floating point numbers are the numbers with fraction or numbers with decimal value for example 34.56, 987.23, 0.234, etc. In Swift, floating point data type has a wide range of values and can store much smaller or larger number as compare to Integer. Swift support two signed floating point number types that is Double and Float. Double is used to represent 64-bit floating point number and has precision of at least 15-decimal digit. Whereas Float is used to represent 32-bit floating point number and has precision of at least 6-decimal digit.

To multiply two floating point number, we will use multiplication operator (*). This operator takes two operand of same type and return their product.

Algorithm to Multiple two float number

  • Step 1 − Define two variables

  • Step 2 − Enter the value both the variables

  • Step 3 − Multiply both the variables with each other

  • Step 4 − Print the output

Example

The following Swift program will show how to multiple two floating point numbers.

import Foundation
import Glibc

let val_1 = 23.4
let val_2 = 34.89

let result = val_1 * val_2

print("Floating point number 1:", val_1)
print("Floating point number 2:", val_2)
print("Product of floating point number:", result)

Output

Floating point number 1: 23.4
Floating point number 2: 34.89
Product of floating point number: 816.4259999999999

Here, we are taking two floating point numbers that is 23.4 and 34.89 and then find their product using multiplication operator(*) that is 816.425999999999.

Algorithm to Multiple two float number from user

  • Step 1 − Define two variables

  • Step 2 − Take the value both the variables from user

  • Step 3 − Multiply both the variables with each other

  • Step 4 − Print the output

Example

The following Swift program will show how to multiple two floating point numbers. Here, we are taking two floating point numbers from the user as an input and then multiply them with each other with the help of multiplication operator(*) and get desired output.

import Foundation
import Glibc

print("Please enter floating point number 1:")
let val1 = Float(readLine()!)!

print("Please enter floating point number 2:")
let val2 = Float(readLine()!)!

let result = val1 * val2
print("Entered Floating point number 1:", val1)
print("Entered Floating point number 2:", val2)
print("Product of floating point number:", result)

Input

Please enter floating point number 1:
2.3
Please enter floating point number 2:
5.6

Output

Entered Floating point number 1: 2.3
Entered Floating point number 2: 5.6
Product of floating point number: 12.879999

In the above code, first we read two floating point numbers from the user(suppose number1 = 2.3 and number2 = 5.6) using the readLine() and Float() functions. Here readLine() function read the data from the user in string form and Float() function convert the string data into floating point data. Then we find their multiplication and assign it to a variable named as result. Display the final result.

Updated on: 28-Jul-2022

455 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements