A complex number is a number that can be expressed in the form a + bi, where a and b are real numbers and i is the imaginary unit, which satisfies the equation i^{2} = -1. In complex number a + bi, a is the real part and b is the imaginary part.
*For example: (2 + 3i) is a complex number.*

## Complex Number Arithmetic

#### Addition of Complex Numbers

Consider two complex numbers C1 = (a + ib) and C2 = (c + id) Let Sum(x + iy) is the sum of C1 and C2 Sum = C1 + C2 (x + iy) = (a + ib) + (c + id) (x + iy) = (a + c) + i(b + d) x = (a + c) and, y = (b + d)

#### Subtraction of Complex Numbers

Let Diff(x + iy) is the difference of C1 and C2 Diff = C1 - C2 (x + iy) = (a + ib) - (c + id) (x + iy) = (a - c) + i(b - d) x = (a - c) and, y = (b - d)

## Importance of Practicing the Program for Adding Complex Numbers for Beginners

**Introduction to Complex Numbers**: This program introduces beginners to the concept of complex numbers in C programming. It demonstrates how to declare, initialize, and perform arithmetic operations on complex numbers.**Understanding Mathematical Concepts**: Implementing this program helps beginners understand mathematical concepts related to complex numbers, such as real and imaginary parts, modulus, and phase angle. It provides hands-on experience with complex number operations.**Improved Programming Skills**: Writing this program improves programming skills by introducing beginners to new data types and operations in C programming. It expands their knowledge and enhances their ability to write complex programs.**Real-world Applications**: Understanding complex numbers is essential for various real-world applications, such as signal processing, electrical engineering, and physics. This program provides a practical example of working with complex numbers in a programming context.**Enhanced Problem-solving Skills**: Writing this program enhances problem-solving skills by challenging beginners to think about how to add complex numbers using C programming concepts. It improves their ability to work with mathematical concepts in a programming environment.

## C program to add two complex numbers using structures

Here we are using a user defined structure 'complexNumber' that contains two integer data members to store real and imaginary part of complex number. A structure provides an encapsulation to represent a complex number as an single entity instead of using two variables to store a complex number.

In this program we take two complex numbers as input from user in the form of A + iB. We will add real parts of input complex number to get the real part of sum complex and add imaginary part of input complex to get imaginary part of sum complex number. Then we print sum complex number.

#include <stdio.h> typedef struct complex { int real; int imaginary; } complexNumber; int main() { complexNumber firstCN, secondCN, sumCN; printf("Enter value of A and B for (A + iB)\n"); scanf("%d %d", &firstCN.real, &firstCN.imaginary); printf("Enter value of C and D for (C + iD)\n"); scanf("%d %d", &secondCN.real, &secondCN.imaginary); /*(A + Bi)+(C + Di) = (A+C) + (B+D)i */ sumCN.real = firstCN.real + secondCN.real; sumCN.imaginary = firstCN.imaginary + secondCN.imaginary; if (sumCN.imaginary >= 0 ) printf("SUM = %d + %di\n", sumCN.real, sumCN.imaginary); else printf("SUM = %d %di\n", sumCN.real, sumCN.imaginary); return 0; }Output

Enter value of A and B for (A + iB) 2 3 Enter value of C and D for (C + iD) 3 4 SUM = 5 + 7i

## C program to subtract two complex numbers using structures

#include <stdio.h> typedef struct complex { int real; int imaginary; } complexNumber; int main() { complexNumber firstCN, secondCN, sumCN; printf("Enter value of A and B for (A + iB)\n"); scanf("%d %d", &firstCN.real, &firstCN.imaginary); printf("Enter value of C and D for (C + iD)\n"); scanf("%d %d", &secondCN.real, &secondCN.imaginary); /* (A + Bi) - (C + Di) = (A-C) + (B-D)i */ sumCN.real = firstCN.real - secondCN.real; sumCN.imaginary = firstCN.imaginary - secondCN.imaginary; if (sumCN.imaginary >= 0 ) printf("DIFFERENCE = %d + %di\n", sumCN.real, sumCN.imaginary); else printf("DIFFERENCE = %d %di\n", sumCN.real, sumCN.imaginary); return 0; }Output

Enter value of A and B for (A + iB) 5 4 Enter value of C and D for (C + iD) 2 2 DIFFERENCE = 3 + 2i

## Tips for Writing the Program for Complex Numbers in C

**Use Structure for Complex Numbers**: Define a structure to represent complex numbers, including real and imaginary parts.**Implement Addition Operation**: Write a function to add two complex numbers by adding their real and imaginary parts separately.**Data Type Compatibility**: Ensure that the data types used for real and imaginary parts are compatible with the operations performed on them.**Precision**: Be mindful of precision when performing arithmetic operations on complex numbers to avoid rounding errors.**Ensure Correct Input**: Validate user input to ensure that the entered values are valid complex numbers.

### Conclusion

In conclusion, the C Program to Add Two Complex Numbers is a valuable exercise that introduces beginners to the concept of complex numbers in C programming. Practicing this program is important for beginners to develop a solid understanding of complex numbers and their applications in programming.

**Related Topics**