Swift Program to get the real part from a Complex number


In this article, we will learn how to write a swift program to get the real part from the complex number. 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. For example, 2+2i, 1-3i, etc. where 2 and 1 are the real part and 2i and -3i is the imaginary part. Here we use the following methods to get the real part −

  • Using the function

  • Without using the function

Method 1: Using the function

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

Algorithm

Step 1 − Create a function that returns the real part.

Step 2 − Create a structure using the struct keyword.

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

Step 4 − Create a method to display the complex number

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

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

Step 7 − Print the output.

Example

Following Swift program to get the real part from the complex number.

import Foundation
import Glibc

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

// 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: 3.4, img: 7.2)

// Displaying complex number
complexNumber.display()

// Finding and displaying real part of the complex number
let real = findRealPart(of: complexNumber)
print("Real part: \(real)")

Output

Complex number: 3.4+7.2i
Real part: 3.4

Here in the above code, first we create a function named findRealPart() which takes the structure object as an argument and returns the real part of the complex number using the 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 parts and a method that will display the complex number. Now we create an instance of a Complex struct and initialize imaginary and real parts. So, call the findRealPart() function and pass the struct instance into it as a parameter and display the real part.

Method 2: Without Using the function

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

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 struct instance and initialize complex number.

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

Step 5 − Print the output.

Example

Following Swift program to get the real part from the 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 real part of the complex number
let realPart = cNumber.real
print("Real part: \(realPart)")

Output

Real part: 4.4

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

Conclusion

Therefore, this is how we can find the real part of the given complex number. Real numbers are the numbers that are present in the number system, they can be positive, negative, zero, rational, irrational, etc.

Updated on: 09-Jan-2023

85 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements