Working with complex numbers is pretty common in many scientific fields. In this program, we’ll add up two complex numbers. We’re using C structures to handle these numbers effectively.
You’ll input two complex numbers, each having a real and an imaginary part, and the program will compute their sum.
Prerequisites for Adding Complex Numbers in C
Before starting, make sure you're familiar with:
- Defining and using
structin C - Functions and parameter passing in C
printfandscanffor input and output- Any C compiler such as GCC 9 or newer — or just run it in the EduSeekho online compiler
Source Code of C Program to Add Two Complex Numbers
Here's the complete code to add two complex numbers using structures and functions.
#include <stdio.h>
typedef struct Complex {
float real;
float imag;
} Complex;
Complex addComplex(Complex c1, Complex c2);
int main() {
Complex c1, c2, result;
// Get first complex number
printf("Enter real and imaginary part of the first complex number:\n");
scanf("%f %f", &c1.real, &c1.imag);
// Get second complex number
printf("Enter real and imaginary part of the second complex number:\n");
scanf("%f %f", &c2.real, &c2.imag);
// Add two complex numbers
result = addComplex(c1, c2);
// Display result
printf("Sum = %.1f + %.1fi\n", result.real, result.imag);
return 0;
}
Complex addComplex(Complex c1, Complex c2) {
Complex result;
result.real = c1.real + c2.real;
result.imag = c1.imag + c2.imag;
return result;
}Try this code yourself — free
Run it in the EduSeekho online compiler. No setup needed.
Output of the Add Two Complex Numbers Program
When you run the program, it asks for two complex numbers and then outputs their sum.
Enter real and imaginary part of the first complex number:
2
3
Enter real and imaginary part of the second complex number:
4
5
Sum = 6 + 8i
Algorithm to Add Two Complex Numbers in C
Adding two complex numbers involves summing up their real and imaginary parts separately. Here is the algorithm the program follows step by step.
- Define a structure
Complexto store real and imaginary parts. - Declare a function
addComplexto add two complex numbers. - In
main(), declare variables for two complex numbers and the result. - Prompt the user to enter real and imaginary parts for both complex numbers.
- Call
addComplex()with the two complex numbers as arguments. - Display the sum of the complex numbers.
flowchart TD A([Start]) --> B[Define structure Complex] B --> C[Declare addComplex function] C --> D[Define addComplex function] D --> E[Declare variables c1, c2, result] E --> F[/Input real and imaginary parts of c1/] F --> G[/Input real and imaginary parts of c2/] G --> H[Call addComplex with c1 and c2] H --> I[Display result] I --> J([End])
How the C Program to Add Two Complex Numbers Works
Let's break down the code to understand how it works.
#include <stdio.h>: This is the standard input-output library forprintfandscanf.typedef struct Complex: Defines aComplexstructure to hold real and imaginary parts.Complex addComplex(Complex c1, Complex c2): Declares a function to add two complex numbers.int main(): The main function where program execution starts.Complex c1, c2, result: Declares variables for the two input complex numbers and their sum.scanf("%f %f", &c1.real, &c1.imag): Reads the real and imaginary parts of the first complex number.result = addComplex(c1, c2): Calls the function to add the two complex numbers.printf("Sum = %.1f + %.1fi\n", result.real, result.imag): Displays the sum of the two complex numbers.return 0: Indicates the program executed successfully.
Quick Quiz — test your understanding
Tap an option to check your answerWhat is the result of adding two complex numbers (2 + 3i) and (4 + 5i) using this program?
Time and Space Complexity of the C Program to Add Two Complex Numbers
Let's discuss the time and space complexity of this program.
Time Complexity: O(1). The operations involved in adding two complex numbers are constant and do not depend on the size of the input. We simply execute a fixed number of additions, as shown in the result.real = c1.real + c2.real line.
Space Complexity: O(1). We only use a fixed amount of memory for the variables c1, c2, and result. These are three instances of the Complex struct, regardless of the input values.
Common mistake: Not returning the result from the addComplex function can mess things up. Always make sure to return the computed value.
Key takeaway: Using structures in C makes managing complex data types way easier.
FAQs About Adding Two Complex Numbers in C
Here are some common questions students have about this topic.
Related Programs
If you found this program helpful, check out these related programs:
- C Program to Add Two Distances in Inch-Feet Using Structures
- C Program to Store Data in Structures Dynamically
- C Program to Swap Two Numbers Using Pointers
- C Program to Store Information of Students Using Structure
- C Program to Find Largest Number Using Dynamic Memory Allocation
Conclusion
Don’t underestimate how useful structures in C can be. They really simplify working with complex data types like complex numbers. Try tweaking this program to do other operations like subtraction or multiplication. It'll really help cement your grasp on structures and functions in C.
Ready to test what you know? Check out our C programming quiz and give this code a spin in our online compiler.