🚀📚Intermediate

Add Two Complex Numbers

Add complex numbers using structures

Complex numbers are those numbers that can be expressed in the form a + bi where a and b are real numbers and i is the imaginary part called "iota". The value of i is √-1.

📝 Example

Input:

a = ( 2 + 3i )

b = ( 4 + 5i )

Output:

Sum = ( 6 + 8i )

Explanation:

= ( 2 + 3i ) + ( 4 + 5i )

= ( 2 + 4 ) + ( 3 + 5 )i

= ( 6 + 8i )

📋 Approach

  1. Define a structure for complex numbers
  2. Define a function for adding two complex numbers
  3. This function accepts two complex numbers as parameters and returns a complex number

📚 What You'll Learn

  • Using struct to define custom data types
  • Using typedef for cleaner code
  • Passing structures to functions
  • Returning structures from functions

C Program to Add Two Complex Numbers

add_complex.c
C
1// C program to demonstrate
2// addition of complex numbers
3#include <stdio.h>
4
5// define a structure for complex number
6typedef struct complexNumber {
7 int real;
8 int img;
9} complex;
10
11// This function accepts two complex numbers
12// as parameter and return addition of them.
13complex add(complex x, complex y);
14
15// Driver code
16int main()
17{
18 // Define three complex type numbers
19 complex a, b, sum;
20
21 // First complex number
22 a.real = 2;
23 a.img = 3;
24
25 // Second complex number
26 b.real = 4;
27 b.img = 5;
28
29 // Print first complex number
30 printf("\n a = %d + %di", a.real, a.img);
31
32 // print second complex number
33 printf("\n b = %d + %di", b.real, b.img);
34
35 // call add(a,b) function and
36 // pass complex numbers a & b
37 // as an parameter.
38 sum = add(a, b);
39
40 // Print result
41 printf("\n sum = %d + %di", sum.real, sum.img);
42
43 return 0;
44}
45
46// Complex add(complex x, complex y)
47// function definition
48complex add(complex x, complex y)
49{
50 // Define a new complex number.
51 complex add;
52
53 // Add real part of a&b
54 add.real = x.real + y.real;
55
56 // Add Imaginary part of a&b
57 add.img = x.img + y.img;
58
59 return (add);
60}
Output

a = 2 + 3i\n b = 4 + 5i\n sum = 6 + 8i

📖 Code Explanation

CodeExplanation
typedef structCreates a new type name for the structure
int real, img;Members to store real and imaginary parts
complex a, b, sum;Declare three complex number variables
a.real = 2;Access structure member using dot operator
sum = add(a, b);Call function and store returned structure
return (add);Return the complex number structure

🏗️ Structure Diagram

struct complexNumber
intreal→ stores real part (e.g., 2)
intimg→ stores imaginary part (e.g., 3)

🔄 How Addition Works

Complex Number a

2 + 3i

real=2, img=3

+

Complex Number b

4 + 5i

real=4, img=5

Result (sum)

6 + 8i

real = 2+4 = 6, img = 3+5 = 8

📐 Complex Number Addition Formula

(a + bi) + (c + di) = (a + c) + (b + d)i

Simply add the real parts together and the imaginary parts together!

Common Mistakes to Avoid

  • ❌ Forgetting to declare the function prototype before main()
  • ❌ Using -> instead of . for structure members
  • ❌ Not initializing structure members before use
  • ❌ Confusing real and imaginary parts during addition

🎯 Key Takeaways

Use struct to group related data
typedef creates custom type names
Structures can be passed to functions
Functions can return structures

Want to Learn More?

Explore our comprehensive tutorials for in-depth explanations of C programming concepts.

Browse Tutorials
Back to All Examples