Program to Add Two Complex Numbers in C

Given are two complex numbers in the form of a1 + ib1 and a2 + ib2, the task is to add these two complex numbers in C programming.

Complex numbers are those numbers which can be expressed in the form of "a + ib" where "a" and "b" are real numbers and "i" is the imaginary unit which satisfies i² = −1. Since no real number satisfies this equation, it is called the imaginary unit.

Syntax

struct complex {
    int real;
    int imaginary;
};

struct complex addComplex(struct complex num1, struct complex num2);

Algorithm

To add two complex numbers, we add their real parts and imaginary parts separately −

  • Declare a structure to store real and imaginary parts
  • Create a function to add two complex numbers
  • Add real parts: result.real = num1.real + num2.real
  • Add imaginary parts: result.imaginary = num1.imaginary + num2.imaginary

Example

Here's a complete program to add two complex numbers using structures −

#include <stdio.h>

// Structure for storing the real and imaginary parts of complex number
struct complexnum {
    int real, img;
};

// Function to add two complex numbers
struct complexnum sumcomplex(struct complexnum a, struct complexnum b) {
    struct complexnum c;
    // Adding real parts
    c.real = a.real + b.real;
    // Adding imaginary parts
    c.img = a.img + b.img;
    return c;
}

int main() {
    struct complexnum a = {3, 8};
    struct complexnum b = {5, 2};
    struct complexnum c = sumcomplex(a, b);
    
    printf("Complex number 1: %d + i%d<br>", a.real, a.img);
    printf("Complex number 2: %d + i%d<br>", b.real, b.img);
    printf("Sum of the complex numbers: %d + i%d<br>", c.real, c.img);
    
    return 0;
}

Output

Complex number 1: 3 + i8
Complex number 2: 5 + i2
Sum of the complex numbers: 8 + i10

Example with User Input

Here's another example that takes user input for complex numbers −

#include <stdio.h>

struct complexnum {
    float real, img;
};

struct complexnum addComplex(struct complexnum num1, struct complexnum num2) {
    struct complexnum result;
    result.real = num1.real + num2.real;
    result.img = num1.img + num2.img;
    return result;
}

int main() {
    struct complexnum num1 = {1.5, 2.7};
    struct complexnum num2 = {3.2, 1.8};
    struct complexnum sum;
    
    sum = addComplex(num1, num2);
    
    printf("First complex number: %.1f + i%.1f<br>", num1.real, num1.img);
    printf("Second complex number: %.1f + i%.1f<br>", num2.real, num2.img);
    printf("Sum: %.1f + i%.1f<br>", sum.real, sum.img);
    
    return 0;
}

Output

First complex number: 1.5 + i2.7
Second complex number: 3.2 + i1.8
Sum: 4.7 + i4.5

Key Points

  • Complex number addition follows the rule: (a + ib) + (c + id) = (a + c) + i(b + d)
  • We use structures in C to represent complex numbers with real and imaginary components
  • The function returns a new complex number containing the sum

Conclusion

Adding complex numbers in C is straightforward using structures. We simply add the real parts together and the imaginary parts together to get the resultant complex number.

Updated on: 2026-03-15T12:58:03+05:30

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements