---
title: "Complex number sum via functions"
subject: "Computer Programming"
chapter: 8
key: "semester_1/computer_programming:struct-complex-sum-function"
source: https://www.bctengineer.com/q/semester_1/computer_programming/struct-complex-sum-function
site: BCT Engineering
---

# Complex number sum via functions

Computer Programming · Chapter 8: Structures

- Asked in: 2068 Shrawan, 10 marks
- Video: [C Programming - Structure Basics : Sum of two complex numbers (using function as well)](https://www.youtube.com/watch?v=qA2QZDD0jsU) — Kamal Gautam
- 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-sum-function
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-8

## Question

Write a program to represent complex number by a structure with real and imaginary as members. Take 2 complex numbers as input from user into structure variables. Pass the complex numbers to a function which calculates sum and returns it. Display the sum from another function display().

## Answer

**Design.**
- The structure `complex` has `real` and `imag` members.
- `main()` reads two complex numbers into the structure variables `c1` and `c2`.
- `add()` takes the two structures (by value), adds the real and imaginary parts separately, and returns the sum as a structure.
- `display()` receives the sum and prints it in the form a + bi.

```c
#include <stdio.h>

struct complex {
    float real;
    float imag;
};

struct complex add(struct complex a, struct complex b)
{
    struct complex s;
    s.real = a.real + b.real;
    s.imag = a.imag + b.imag;
    return s;
}

void display(struct complex c)
{
    printf("Sum = %.1f %c %.1fi\n", c.real, c.imag < 0 ? '-' : '+',
           c.imag < 0 ? -c.imag : c.imag);
}

int main(void)
{
    struct complex c1, c2, sum;
    printf("Enter first complex number (real imaginary): ");
    scanf("%f %f", &c1.real, &c1.imag);
    printf("Enter second complex number (real imaginary): ");
    scanf("%f %f", &c2.real, &c2.imag);
    sum = add(c1, c2);
    display(sum);
    return 0;
}
```

**Sample run** (3 + 4i and 1.5 − 6i)

Input: `3 4 1.5 -6`

```text
Enter first complex number (real imaginary): Enter second complex number (real imaginary): Sum = 4.5 - 2.0i
```

(3 + 1.5) + (4 − 6)i = 4.5 − 2i.

## Check yourself

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

1. How does add() calculate the sum?
   A. Multiplies the parts
   B. Adds real parts and imaginary parts separately
   C. Adds real to imaginary
   D. Subtracts the parts
   Answer: B. s.real = a.real+b.real; s.imag = a.imag+b.imag. (Key point: Sum rule)

2. How are the complex numbers passed to add()?
   A. Only as ints
   B. As global arrays
   C. By value as structures
   D. As strings
   Answer: C. Copies of c1 and c2 are passed. (Key point: Passing)

3. What does add() return?
   A. void
   B. An int
   C. A char
   D. A struct complex holding the sum
   Answer: D. Structures can be returned. (Key point: Return)

4. Which function displays the sum?
   A. display(), which receives the sum structure
   B. main() only
   C. add()
   D. scanf
   Answer: A. The question asks for display() separately. (Key point: Display)

5. 3+4i plus 1.5-6i =?
   A. 4.5 + 10i
   B. 4.5 - 2i
   C. 4.5 - 10i
   D. 1.5 - 2i
   Answer: B. (3+1.5)=4.5 and (4-6)=-2. (Key point: Sample)

## Related in Computer Programming

- [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
- [Complex number multiplication using structure](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astruct-complex-multiply) — Computer Programming, chapter 8
- [Sending structure members to a function](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astructure-members-to-function) — Computer Programming, chapter 8
- [Insert a string into another string](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Ainsert-string-into-string) — Computer Programming, chapter 7
- [Pass student array; display passed students](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apass-whole-array-student-info) — Computer Programming, chapter 7
- [GOTO types; sum of series program](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-goto-types-series) — Computer Programming, chapter 11

## Related in other subjects

- [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
- [LandMeasure class and sum function](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Alandmeasure-class-sum) — Object Oriented Programming, chapter 4
- [Complex numbers relational operators](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Acomplex-relational-overload) — Object Oriented Programming, chapter 5
- [8086 ALP: sum of 100 even numbers](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8086-sum-even-numbers) — Microprocessor, chapter 3
