Swift Program to get the imaginary part of the given complex number


In this article, we will learn how to write a swift program to get the imaginary part of the given complex number. Complex numbers are those number which express in the form of x+yi, where x and y are real numbers and ‘i’ is an imaginary number known as iota. For example: 1+2i, 1-3i, etc. Here we use the following methods to get imaginary part −

  • Using function

  • Without using function

Method 1: Using function

To get the imaginary part of the complex number we create a function which takes struct object as a parameter and return the imaginary part of the given complex number.

Algorithm

Step 1 − Create a function which return imaginary part.

Step 2 − Create a structure using struct keyword.

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

Step 4 − Create a method to display complex number

Step 5 − Create an struct instance and initialise complex number.

Step 6 − Call the function to find imaginary part of the complex number.

Step 7 − Print the output.

Example

Following Swift program to get the imaginary part of the given complex number.

import Foundation
import Glibc

// Function to find the imaginary part of the complex number
func findImaginaryPart(of complexNumber: Complex) ->
Double {
   return complexNumber.img
}

// Structure to create complex number
struct Complex {
   var real: Double
   var img: Double
   func display() {
      print("Complex number: \(real)+\(img)i")
   }
}

// Initializing complex number
let complexNumber = Complex(real: 4.4, img: 5.1)

// Displaying complex number
complexNumber.display()

// Finding and displaying imaginary part of the complex number
let imaginary = findImaginaryPart(of: complexNumber)
print("Imaginary part: \(imaginary)")

Output

Complex number: 4.4+5.1i
Imaginary part: 5.1

Here in the above code, first we create a function named findImaginaryPart() which takes the structure object as an argument and return imaginary part of the complex number using instance name, dot operator and property name. Now we create a struct for a complex number in which we declare two properties of double type to store real and imaginary part and a method which will display complex number. Now we create an instance of Complex struct and initialise imaginary and real part. So, call the findImaginaryPart() function and pass the struct instance into it as a parameter and display imaginary part.

Method 2: Without Using function

To get the imaginary part of the complex number we simply create a complex number using struct and store real and imaginary part in the properties of the struct. Now using dot operator we access the imaginary part of the complex number.

Algorithm

Step 1 − Create a structure using struct keyword.

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

Step 3 − Create an struct instance and initialise complex number.

Step 4 − Using dot operator we access imaginary part and store into a new variable.

Step 5 − Print the output.

Example

Following Swift program to get the imaginary part of the given complex number.

import Foundation
import Glibc

// Structure to create complex number
struct Complex {
   var real: Double
   var img: Double
}

// Initializing complex number
let cNumber = Complex(real: 4.4, img: 7.79)

// Finding imaginary part of the complex number
let imaginaryNumber = cNumber.img
print("Imaginary part: \(imaginaryNumber)")

Output

Imaginary part: 7.79

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 part of the complex number. Now we create a struct instance and initialise real with 4.4 and imaginary with 7.79. To print the imaginary part of the complex number we access the img property of the Complex struct using dot operator along with struct instance like cNumber.img.

Conclusion

So this is how we can find the imaginary part of the given complex number. Imaginary numbers are not a real numbers and when we square the imaginary number we get negative value.

Updated on: 09-Jan-2023

75 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements