# Haskell Program to convert a number into a complex number

This tutorial will help us in converting a number into a complex number. In Haskell, the Data.Complex library provides a Complex type to represent complex numbers. The :+ operator is used to construct a complex number from its real and imaginary parts.

## Method 1: Using convertToComplex Function

In this approach, the convertToComplex function takes a real number as input and returns a complex number with the real component equal to the sum of the input and the predefinedReal value and the imaginary component equal to the predefinedImaginary value. The main function gets a real number and then uses convertToComplex to convert it to a complex number, which is then printed to the screen.

### Algorithm

• Step 1 − ‘Data.Complex’ module is imported.

• Step 2 − Predefined real and imaginary values are defined.

• Step 3 − convertToComplex() function is defined using predefined real and imaginary values, as (predefinedReal + real) :+ predefinedImaginary

• Step 4 − Program execution will be started from main function. The main() function has whole control of the program.

• Step 5 − A variable named, “real” is being initialized. It will have the real number value that is to be converted into a complex number.

• Step 6 − convertToComplex() function is being called and real value will be passed as parameter to it.

• Step 7 − Final resultant converted complex number value is displayed.

### Example

In the following example, we are going to learn how to use convertToComplex function to convert a number into complex number

import Data.Complex

predefinedReal :: Double
predefinedReal = 2.0
predefinedImaginary :: Double
predefinedImaginary = 3.0

convertToComplex :: Double -> Complex Double
convertToComplex real = (predefinedReal + real) :+ predefinedImaginary

main :: IO ()
main = do
let real = 4.0
let complex = convertToComplex real


### Output

Complex number: 4.0 :+ 3.0


## Method 3: Using the :+ operator

In this method, we can use the :+ operator to create a complex number, which takes a real number as the first argument and an imaginary number as the second argument.

### Algorithm

• Step 1 − ‘Data.Complex’ module is imported.

• Step 2 − convertToComplex() function is defined using real and imaginary values, as real :+ imaginary

• Step 3 − Program execution will be started from main function. The main() function has whole control of the program. It is written as, main = do.

• Step 4 − The variables named, “real” and “imaginary” are being initialized. They will have the real and imaginary number value respectively, that is to be converted into a complex number.

• Step 5 − convertToComplex() function is being called and real & imaginary value will be passed as parameter to it.

• Step 6 − Final resultant converted complex number value is displayed.

### Example

In the following example, we are going to learn how to use :+ operator to convert a number into complex number

import Data.Complex

convertToComplex :: Double -> Double -> Complex Double
convertToComplex real imaginary = real :+ imaginary

main :: IO ()
main = do
let real = 4.0
let imaginary = 3.0
let complex = convertToComplex real imaginary
putStrLn \$ "Complex number: " ++ show complex


### Output

Complex number: 4.0 :+ 3.0


## Conclusion

In Haskell, the number can be converted into complex number by various methods including convertToComplex methods using mkPolar, rectangular functionc, etc.

In every approach, the real number that needs to be converted is passed and once you have entered the real number, it will add the predefined real and imaginary values to the input real number, then the resulting complex number will be printed to the screen in the form of (real :+ imaginary).

Updated on: 20-Jan-2023

99 Views