C Program to Add Two Complex Numbers

In this article, we will write a C program to add two complex numbers using structures. The program takes the real and imaginary parts of both complex numbers from the user as input, adds both complex numbers and prints the result on the output window.

Sample Example:

Enter the real and imaginary parts of the first complex number: 10 20
Enter the real and imaginary parts of the second complex number: 15 25
Sum of both complex number = (25.00 + 45.00i)

A complex number is a number that comprises a real part and an imaginary part. It is expressed in the form of a + bi, where:

  • a is the real part (a real number).
  • b is the coefficient of the imaginary part. It is also a real number.
  • i is the imaginary unit, which is defined as the square root of -1.

To add two complex numbers (a + bi) and (c + di) you can simply add their real parts separately from their imaginary parts. The result will be a new complex number with a real part and an imaginary part. The formula for adding two complex numbers is as follows:

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

For example, if there are two complex numbers (1 + 2i) and (5 + 10i), their sum will be a new complex number (6 + 12i).

In C programming, we generally use structures to create complex numbers as a whole. Where each structure will have a real and an imaginary part representing the real and imaginary part of the complex number.

See implementation in the following program:

// C program to add two complex numbers

#include <stdio.h>

//Define a custom type using structures
typedef struct complex{
	float real;
	float imag;
} complex;

// Function to add complex numbers
complex addComplex(complex a, complex b){
	
	// Create a new complex number to store result
	complex res;
	
	// Add the real and imaginary parts
	res.real = a.real + b.real;
	res.imag = a.imag + b.imag;
	
	return res;
}

int main() {
	
	complex c1, c2, result;
	
	printf("Enter the real and imaginary parts of the first complex number: ");
	scanf("%f %f", &c1.real, &c1.imag);
	
	printf("Enter the real and imaginary parts of the second complex number: ");
	scanf("%f %f", &c2.real, &c2.imag);
	
	// Call the function
	result = addComplex(c1, c2);
	
	// Print the result
	printf("Sum of both complex number = (%.2f + %.2fi)", result.real, result.imag);
	
	
    return 0;

}

Output:

Enter the real and imaginary parts of the first complex number: 10 20
Enter the real and imaginary parts of the second complex number: 30 40
Sum of both complex number = (40.00 + 60.00i)

In the above program, we created a custom data type complex using C structures and the typedef keyword. You can use this data type just like C built-in data types such as int or float.

So when you make any variable of type complex, it will always have a real and an imaginary part. Which you can access using the dot(.) operator.

We took the value of the real and imaginary parts of both complex numbers from the user and passed both of them to the addComplex() method which adds their real and imaginary parts and returns a new complex number as a result.

The resulting complex number is then printed to the output screen. That’s how this program works.

I hope you will find this post helpful. Thanks for reading.

Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.

    View all posts

Leave a Comment