# 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

68 Views
{"ft_lang_label":"__label__en","ft_lang_prob":0.7080637,"math_prob":0.9879495,"size":4762,"snap":"2023-40-2023-50","text_gpt3_token_len":1087,"char_repetition_ratio":0.2356032,"word_repetition_ratio":0.26987952,"special_character_ratio":0.2299454,"punctuation_ratio":0.098173514,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9946688,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-09-22T04:02:38Z\",\"WARC-Record-ID\":\"<urn:uuid:56a950b0-1173-48ef-93dc-d1ead4f2e7e6>\",\"Content-Length\":\"74098\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:c80cd812-40cb-4053-8f79-b501ea46a9d6>\",\"WARC-Concurrent-To\":\"<urn:uuid:388d9cec-6e36-46c3-a914-2d9a39ac2308>\",\"WARC-IP-Address\":\"192.229.210.176\",\"WARC-Target-URI\":\"https://www.tutorialspoint.com/swift-program-to-get-the-real-part-from-a-complex-number\",\"WARC-Payload-Digest\":\"sha1:PGJKT3G7CY7V7VAB52BA7Y3O6WSIY2W3\",\"WARC-Block-Digest\":\"sha1:NSNMZRAIDZIER7KDXFSQZXRLQNKGQAMO\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233506329.15_warc_CC-MAIN-20230922034112-20230922064112-00738.warc.gz\"}"}