C Program to Add Two Complex Numbers
C Program to Add Two Complex Numbers
A school exercise asks you to add complex numbers. Use small, named values first so you can distinguish the required calculation from the syntax used to express it.
Add complex numbers by separately adding their real parts and their imaginary coefficients.
Before this exercise, review arithmetic and conversions and numeric types and their limits. The worked program below applies those ideas to this specific problem.
Explain
Explain why add complex numbers uses this sequence of operations.
Trace
Trace the sample values and predict the printed result.
Apply
Add 1+2i and 3+4i.
Add Complex Numbers: follow the data
Given data
A school exercise asks you to add complex numbers. Use small, named values first so you can distinguish the required calculation from the syntax used to express it.
Rule
Add complex numbers by separately adding their real parts and their imaginary coefficients.
Observed result
Sum: 6.0 + 4.0i
Understand the operation
The pair (2,3) represents 2+3i; it is a structure with named double members, not string text or two independent roots.
Read the declarations and operands before the output call. %d formats an int; floating-point results use an appropriate precision with %f. A format describes a value that was already computed—it does not choose the earlier arithmetic.
Read the complete C program
The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.
C17 · complete program
#include <stdio.h>
struct Complex { double real; double imaginary; };
int main(void) {
struct Complex first = {2.0, 3.0}, second = {4.0, 1.0};
struct Complex sum = {first.real + second.real, first.imaginary + second.imaginary};
printf("Sum: %.1f + %.1fi\n", sum.real, sum.imaginary);
return 0;
}
Expected output
Sum: 6.0 + 4.0i
Run in C compiler →Follow the values through the code
C17 · code used in this explanation
#include <stdio.h>
struct Complex { double real; double imaginary; };
int main(void) {
struct Complex first = {2.0, 3.0}, second = {4.0, 1.0};
struct Complex sum = {first.real + second.real, first.imaginary + second.imaginary};
printf("Sum: %.1f + %.1fi\n", sum.real, sum.imaginary);
return 0;
}
Real parts add as 2+4=6.
Imaginary coefficients add as 3+1=4.
Follow the worked calculation
Start with the sample
Real parts add as 2+4=6.
Apply the rule
Imaginary coefficients add as 3+1=4.
Check the result
Sum: 6.0 + 4.0i
Sum: 6.0 + 4.0i
Try a small variation
C17 · code used in this explanation
#include <stdio.h>
struct Complex { double real; double imaginary; };
int main(void) {
struct Complex first = {2.0, 3.0}, second = {4.0, 1.0};
struct Complex sum = {first.real + second.real, first.imaginary + second.imaginary};
printf("Sum: %.1f + %.1fi\n", sum.real, sum.imaginary);
return 0;
}
Add 1+2i and 3+4i.
Worked answer
The worked answer follows the same rule with the requested changed data. Compare its complete source and output: Sum: 4.0 + 6.0i. The result should follow from the operations, rather than from memorizing the first sample.
C17 · complete program
#include <stdio.h>
struct Complex { double real; double imaginary; };
int main(void) {
struct Complex first = {1.0, 2.0}, second = {3.0, 4.0};
struct Complex sum = {first.real + second.real, first.imaginary + second.imaginary};
printf("Sum: %.1f + %.1fi\n", sum.real, sum.imaginary);
return 0;
}
Expected output
Sum: 4.0 + 6.0i
Run in C compiler →Recognize the important boundaries
This structure is a teaching representation, not a full complex-number library. C also provides complex arithmetic through complex.h; formatting must handle negative imaginary coefficients appropriately.
The demonstrated values stay within the types’ representable ranges. A program accepting arbitrary external values needs checked conversion and bounds before performing arithmetic.
Check your understanding
Choose the answer, then compare the reasoning for every option.
Use the idea independently
Add complex numbers by separately adding their real parts and their imaginary coefficients.
Continue with arithmetic operand types.
Continue with validated input.
Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.
Reaching the recap and completing the quizzes are separate milestones.