C Program to Add Two Complex Numbers by Passing Structure to a Function

Adding two complex numbers in C can be efficiently handled using structures and functions. This article will cover three different C Program to Add Two Complex Numbers by Passing Structure to a Function, Each method will be explained in detail with example programs and outputs.

Prerequisites

Before diving into the examples, ensure you have the following prerequisites:

  • Basic understanding of C programming.
  • Familiarity with structures in C.
  • A C compiler installed on your system (e.g., GCC).

Solution 1: Basic Addition of Two Complex Numbers

1.1 Explanation

In this approach, we define a structure to represent a complex number with real and imaginary parts. We then create a function to add two complex numbers and return the result.

1.2 Program

C
#include <stdio.h>

struct Complex {
    float real;
    float imag;
};

// Function to add two complex numbers
struct Complex addComplex(struct Complex c1, struct Complex c2) {
    struct Complex result;
    result.real = c1.real + c2.real;
    result.imag = c1.imag + c2.imag;
    return result;
}

int main() {
    struct Complex c1, c2, result;

    // Input first complex number
    printf("Enter real and imaginary parts of the first complex number: ");
    scanf("%f %f", &c1.real, &c1.imag);

    // Input second complex number
    printf("Enter real and imaginary parts of the second complex number: ");
    scanf("%f %f", &c2.real, &c2.imag);

    // Add the complex numbers
    result = addComplex(c1, c2);

    // Display the result
    printf("Sum of the complex numbers: %.2f + %.2fi\n", result.real, result.imag);

    return 0;
}

1.3 Output

C
Enter real and imaginary parts of the first complex number: 3.2 4.5
Enter real and imaginary parts of the second complex number: 1.3 2.4
Sum of the complex numbers: 4.50 + 6.90i

Solution 2: Using Pointers to Add Complex Numbers

2.1 Explanation

This approach uses pointers to pass structures to the addition function. This method is useful for modifying the original data or when dealing with larger structures.

2.2 Program

C
#include <stdio.h>

struct Complex {
    float real;
    float imag;
};

// Function to add two complex numbers using pointers
void addComplex(struct Complex *c1, struct Complex *c2, struct Complex *result) {
    result->real = c1->real + c2->real;
    result->imag = c1->imag + c2->imag;
}

int main() {
    struct Complex c1, c2, result;

    // Input first complex number
    printf("Enter real and imaginary parts of the first complex number: ");
    scanf("%f %f", &c1.real, &c1.imag);

    // Input second complex number
    printf("Enter real and imaginary parts of the second complex number: ");
    scanf("%f %f", &c2.real, &c2.imag);

    // Add the complex numbers using pointers
    addComplex(&c1, &c2, &result);

    // Display the result
    printf("Sum of the complex numbers: %.2f + %.2fi\n", result.real, result.imag);

    return 0;
}

Output

Enter real and imaginary parts of the first complex number: 5.1 3.3
Enter real and imaginary parts of the second complex number: 2.4 4.7
Sum of the complex numbers: 7.50 + 8.00i

Solution 3: Using Array of Structures

3.1 Explanation

In this example, we demonstrate how to handle multiple pairs of complex numbers using an array of structures. We will loop through the array to input, add, and display the complex numbers.

3.2 Program

C
#include <stdio.h>

struct Complex {
    float real;
    float imag;
};

// Function to add two complex numbers
struct Complex addComplex(struct Complex c1, struct Complex c2) {
    struct Complex result;
    result.real = c1.real + c2.real;
    result.imag = c1.imag + c2.imag;
    return result;
}

int main() {
    struct Complex complexPairs[3][2], results[3];

    // Input complex numbers for each pair
    for (int i = 0; i < 3; i++) {
        printf("Enter real and imaginary parts of the first complex number of pair %d: ", i+1);
        scanf("%f %f", &complexPairs[i][0].real, &complexPairs[i][0].imag);

        printf("Enter real and imaginary parts of the second complex number of pair %d: ", i+1);
        scanf("%f %f", &complexPairs[i][1].real, &complexPairs[i][1].imag);

        // Add the complex numbers
        results[i] = addComplex(complexPairs[i][0], complexPairs[i][1]);
    }

    // Display the results
    for (int i = 0; i < 3; i++) {
        printf("Sum of complex numbers for pair %d: %.2f + %.2fi\n", i+1, results[i].real, results[i].imag);
    }

    return 0;
}

Output

C
Enter real and imaginary parts of the first complex number of pair 1: 3.5 4.5
Enter real and imaginary parts of the second complex number of pair 1: 2.5 3.5
Enter real and imaginary parts of the first complex number of pair 2: 1.1 2.2
Enter real and imaginary parts of the second complex number of pair 2: 3.3 4.4
Enter real and imaginary parts of the first complex number of pair 3: 0.5 1.5
Enter real and imaginary parts of the second complex number of pair 3: 1.5 2.5

Sum of complex numbers for pair 1: 6.00 + 8.00i
Sum of complex numbers for pair 2: 4.40 + 6.60i
Sum of complex numbers for pair 3: 2.00 + 4.00i

Conclusion

Using C Program to Add Two Complex Numbers by Passing Structure to a Function to manage and manipulate complex numbers can significantly enhance code readability and maintainability. This article presented three different methods to add two complex numbers using structures: a basic approach, using pointers, and handling multiple pairs of complex numbers with arrays. Each method serves different needs and can be chosen based on the specific requirements of your application. By understanding these techniques, you can effectively manage structured data in your C programs, making complex number operations straightforward and efficient.