🏗️📚Intermediate
Add Complex Numbers by Passing Structure
Complex number addition
Complex numbers have real and imaginary parts. A structure is perfect for grouping these two values.
Program Code
struct_complex.c
C
1#include <stdio.h>23struct Complex {4 float real;5 float imag;6};78struct Complex addComplex(struct Complex c1, struct Complex c2) {9 struct Complex result;10 result.real = c1.real + c2.real;11 result.imag = c1.imag + c2.imag;12 return result;13}1415int main() {16 struct Complex num1 = {3.5, 2.5};17 struct Complex num2 = {1.5, 4.5};18 19 struct Complex sum = addComplex(num1, num2);20 21 printf("First: %.1f + %.1fi\n", num1.real, num1.imag);22 printf("Second: %.1f + %.1fi\n", num2.real, num2.imag);23 printf("Sum: %.1f + %.1fi\n", sum.real, sum.imag);24 25 return 0;26}Output
First: 3.5 + 2.5i
Second: 1.5 + 4.5i
Sum: 5.0 + 7.0i
Related Examples
Want to Learn More?
Explore our comprehensive tutorials for in-depth explanations of C programming concepts.
Browse Tutorials