In this tutorial, we will learn how to write a C program to multiply two complex numbers using structure.

## C Program to find the product of two complex numbers using structure

The explanation of the program is at the end of this code.

#include <stdio.h>

// Structure to store a complex number

struct Complex {

float real; // Real part of the complex number

float imag; // Imaginary part of the complex number

};

// Function to multiply two complex numbers

struct Complex multiplyComplex(struct Complex c1, struct Complex c2) {

struct Complex result;

// Calculate the real part of the product

result.real = c1.real * c2.real - c1.imag * c2.imag;

// Calculate the imaginary part of the product

result.imag = c1.real * c2.imag + c1.imag * c2.real;

return result; // Return the resulting complex number

}

int main() {

struct Complex c1, c2, result;

// Input first complex number

printf("Enter real and imaginary parts of the first complex number: ");

scanf("%f %f", &c1.real, &c1.imag);

// Input second complex number

printf("Enter real and imaginary parts of the second complex number: ");

scanf("%f %f", &c2.real, &c2.imag);

// Multiply the complex numbers

result = multiplyComplex(c1, c2);

// Output the result

printf("Product of the complex numbers: %.2f + %.2fi\n", result.real, result.imag);

return 0;

}

Output:

Enter real and imaginary parts of the first complex number: 3 2

Enter real and imaginary parts of the second complex number: 1 7

Product of the complex numbers: -11.00 + 23.00i

### Explanation of output:

- The user is prompted to enter the real and imaginary parts of the first complex number. The user inputs
`3`

and`2`

. - The user is prompted to enter the real and imaginary parts of the second complex number. The user inputs
`1`

and`7`

. - The program calculates the product of the complex numbers:
- Real part:
`(3 * 1 - 2 * 7) = 3 - 14 = -11`

- Imaginary part:
`(3 * 7 + 2 * 1) = 21 + 2 = 23`

- Real part:
- The result
`-11.00 + 23.00i`

is printed.

## How this program works?

**1. Include Standard Input-Output Library**:

#include <stdio.h>

This includes the standard input-output library needed for `printf`

and `scanf`

functions.

**2. Define the Structure:**

struct Complex {

float real; // Real part of the complex number

float imag; // Imaginary part of the complex number

};

The `struct Complex`

is defined to store a complex number with `real`

and `imag`

parts. For example, In the complex number 3 + 4i, the real part is 3 and imaginary part is 4.

**3. Function to Multiply Complex Numbers**:

struct Complex multiplyComplex(struct Complex c1, struct Complex c2) {

struct Complex result;

// Calculate the real part of the product

result.real = c1.real * c2.real - c1.imag * c2.imag;

// Calculate the imaginary part of the product

result.imag = c1.real * c2.imag + c1.imag * c2.real;

return result; // Return the resulting complex number

}

- The function
`multiplyComplex`

takes two`struct Complex`

arguments and returns their product. - The real part of the result is calculated as
`(c1.real * c2.real - c1.imag * c2.imag)`

. - The imaginary part of the result is calculated as
`(c1.real * c2.imag + c1.imag * c2.real)`

.

**4. Main Function**:

`struct Complex c1, c2, result;`

declares three`Complex`

variables.`printf`

and`scanf`

are used to take input from the user and store the entered complex numbers in structures`c1`

and`c2`

.`result = multiplyComplex(c1, c2);`

calls the function to multiply the complex numbers.`printf("Product of the complex numbers: %.2f + %.2fi\n", result.real, result.imag);`

prints the result.

## Leave a Reply