---
title: "Complex number multiplication using structure"
subject: "Computer Programming"
chapter: 8
key: "semester_1/computer_programming:struct-complex-multiply"
source: https://www.bctengineer.com/q/semester_1/computer_programming/struct-complex-multiply
site: BCT Engineering
---

# Complex number multiplication using structure

Computer Programming · Chapter 8: Structures

- Asked in: 2074 Asoj, 5 marks
- Video: [C Program to perform arithmetic operations on complex number using structure](https://www.youtube.com/watch?v=j43XC2U1TyQ) — Anita Devkar
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/struct-complex-multiply
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-8

## Question

Create a structure to hold any complex number x+iy. Write a program that uses the structure to read two complex numbers and display a third complex number which is the multiplication of the entered complex numbers.

## Short answer

Product of (x1+iy1) and (x2+iy2): real = x1*x2 - y1*y2, imag = x1*y2 + y1*x2. For input 2 3 and 4 -1, the result is 11.00 + 10.00i.

## Answer

For two complex numbers (x1 + i y1) and (x2 + i y2):

(x1 + i y1)(x2 + i y2) = (x1x2 − y1y2) + i (x1y2 + y1x2), since i² = −1.

The structure `complex` has two float members, `x` (real part) and `y` (imaginary part).

```c
#include <stdio.h>

struct complex {
    float x;       /* real part */
    float y;       /* imaginary part */
};

int main(void)
{
    struct complex a, b, p;

    printf("Enter first complex number (x y): ");
    scanf("%f %f", &a.x, &a.y);
    printf("Enter second complex number (x y): ");
    scanf("%f %f", &b.x, &b.y);

    p.x = a.x * b.x - a.y * b.y;          /* (x1x2 - y1y2) */
    p.y = a.x * b.y + a.y * b.x;          /* (x1y2 + y1x2) */

    printf("Product = %.2f %c %.2fi\n", p.x, p.y < 0 ? '-' : '+', p.y < 0 ? -p.y : p.y);
    return 0;
}
```

**Sample run** (2 + 3i and 4 − i)

Input: `2 3 4 -1`

```text
Enter first complex number (x y): Enter second complex number (x y): Product = 11.00 + 10.00i
```

Check: (2)(4) − (3)(−1) = 11 and (2)(−1) + (3)(4) = 10, so the product is 11 + 10i.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What is the product of (x1+iy1)(x2+iy2)?
   A. (x1x2 - y1y2) + i(x1y2 + y1x2)
   B. (x1x2 + y1y2) + i(x1y2 - y1x2)
   C. (x1+x2) + i(y1+y2)
   D. (x1x2) + i(y1y2)
   Answer: A. i^2 = -1 gives the minus in the real part. (Key point: Formula)

2. Why does the real part have -y1y2?
   A. Because y is negative
   B. Because i^2 = -1
   C. Because of floats
   D. Because of scanf
   Answer: B. The i*i term becomes -1. (Key point: Reason)

3. What members does struct complex have?
   A. One int
   B. Two chars
   C. Two floats: x (real) and y (imaginary)
   D. Three floats
   Answer: C. It models x + iy. (Key point: Structure)

4. (2+3i)(4-i) = ?
   A. 5 + 2i
   B. 8 - 3i
   C. 11 - 10i
   D. 11 + 10i
   Answer: D. Real 8+3 = 11, imaginary -2+12 = 10. (Key point: Sample)

5. Why is the sign printed with a conditional (p.y < 0 ? '-' : '+')?
   A. To display negative imaginary part as - and use its absolute value
   B. To round the value
   C. To swap parts
   D. To avoid scanf
   Answer: A. It prints 'a - bi' neatly. (Key point: Sign printing)

## Related in Computer Programming

- [Complex number sum via functions](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astruct-complex-sum-function) — Computer Programming, chapter 8
- [Batsman average using structure](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astruct-batsman-average) — Computer Programming, chapter 8
- [Read records until end; show nth record](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-worker-nth-record) — Computer Programming, chapter 10, asked 2×
- [Sum and average until 0 is entered](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Asum-average-until-zero) — Computer Programming, chapter 5
- [Function definition; sum of two numbers](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afunction-definition-sum-two) — Computer Programming, chapter 6
- [Largest of n numbers in FORTRAN](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-largest-n-numbers) — Computer Programming, chapter 11

## Related in other subjects

- [8086 ALP: multiplication table of a number](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8086-multiplication-table) — Microprocessor, chapter 3
- [C program: sum of two 8-bit BCD numbers](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3A8051-bcd-sum-c) — Embedded System, chapter 9
- [Complex numbers relational operators](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Acomplex-relational-overload) — Object Oriented Programming, chapter 5
- [8085 ALP: multiplication table of a number](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8085-multiplication-table) — Microprocessor, chapter 2
