🚀📚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
- Define a structure for complex numbers
- Define a function for adding two complex numbers
- This function accepts two complex numbers as parameters and returns a complex number
📚 What You'll Learn
- •Using
structto define custom data types - •Using
typedeffor 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 demonstrate2// addition of complex numbers3#include <stdio.h>45// define a structure for complex number6typedef struct complexNumber {7 int real;8 int img;9} complex;1011// This function accepts two complex numbers12// as parameter and return addition of them.13complex add(complex x, complex y);1415// Driver code16int main()17{18 // Define three complex type numbers19 complex a, b, sum;2021 // First complex number22 a.real = 2;23 a.img = 3;2425 // Second complex number26 b.real = 4;27 b.img = 5;2829 // Print first complex number30 printf("\n a = %d + %di", a.real, a.img);3132 // print second complex number33 printf("\n b = %d + %di", b.real, b.img);3435 // call add(a,b) function and36 // pass complex numbers a & b37 // as an parameter.38 sum = add(a, b);3940 // Print result41 printf("\n sum = %d + %di", sum.real, sum.img);4243 return 0;44}4546// Complex add(complex x, complex y)47// function definition48complex add(complex x, complex y)49{50 // Define a new complex number.51 complex add;5253 // Add real part of a&b54 add.real = x.real + y.real;5556 // Add Imaginary part of a&b57 add.img = x.img + y.img;5859 return (add);60}Output
a = 2 + 3i\n b = 4 + 5i\n sum = 6 + 8i
📖 Code Explanation
| Code | Explanation |
|---|---|
| typedef struct | Creates 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
Related Examples
Want to Learn More?
Explore our comprehensive tutorials for in-depth explanations of C programming concepts.
Browse Tutorials