Swift Program to initialize and print a complex number


In this article, we will learn how to write a swift program to initialize and print complex numbers. Complex numbers are those numbers which express in the form of x+yi, where x and y are real numbers and ‘i’ is an imaginary number known as iota. Alternatively, we can say that a complex number is a combination of both real and imaginary numbers. For example 1+2i, 1-3i, etc. In Swift, we use a struct to create complex numbers.

Algorithm

Step 1 − Create a structure using the struct keyword.

Step 2 − In this structure, create two properties of double type to store real and imaginary parts of the complex number.

Step 3 − Create a method to display complex numbers.

Step 4 − Create a struct instance and initialize a complex number.

Step 5 − using the dot operator, we access the method of the struct.

Step 6 − Print the output.

Example

Following the Swift program to initialize and print complex numbers.

import Foundation
import Glibc

// Creating complex number using structure
struct ComplexNumber {
   var real: Double
   var imaginary: Double

   // Method to display complex number
   func Display() {
      print("Complex number:")
      print("\(real) + \(imaginary)i")
   }
}

// Initialize and print a complex number
let obj = ComplexNumber(real: 5, imaginary: 7)
obj.Display()

Output

Complex number:
5.0 + 7.0i

Here in the above code, we create a complex number using structure. In this struct, we declare two properties of double type to store real and imaginary parts of the complex number and a method named Display() to display complex numbers. Now we create a struct instance and initialize real with 5.0 and imaginary with 7.0. To print the complex number we call the Display() method of the struct using the dot operator along with struct instance like obj.Display().

Example

Following Swift program to initialize and print complex number.

import Foundation
import Glibc

// Creating comples number using structure
struct ComplexNumber {
   var real: Double
   var imaginary: Double
   init(real: Double, imaginary: Double) {
      self.real = real
      self.imaginary = imaginary
   }

   // Method to display complex number
   func Show() {
      print("\(real) + \(imaginary)i")
   }
}

// Initialize and print a complex number
let obj = ComplexNumber(real: 4, imaginary: 8)
obj.Show() 

Output

4.0 + 8.0i

Here in the above code, we create a complex number using structure. In this struct, we declare two properties of double type to store real and imaginary parts of the complex number, an initializer function that makes it easier to create complex numbers, and a method named Display() to display complex numbers. Here initializer function is similar to constructors in Java. It is used to set the initial value of the properties of the struct or can perform other setups before the new instance is ready for the user.

Conclusion

Therefore, this is how we can initialize and print complex numbers. Swift does not provide any library functions to create a complex number so we use structure to create and initialize complex numbers.

Updated on: 09-Jan-2023

166 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements