w3resource

C Program structure: Complex number operations


8. Complex Number Operations

Create a structure named Complex to represent a complex number with real and imaginary parts. Write a C program to add and multiply two complex numbers.

Sample Solution:

C Code:

#include <stdio.h>

// Define the structure "Complex"
struct Complex {
    float real;
    float imag;
};

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

// Function to multiply two complex numbers
struct Complex multiplyComplex(struct Complex num1, struct Complex num2) {
    struct Complex result;
    result.real = (num1.real * num2.real) - (num1.imag * num2.imag);
    result.imag = (num1.real * num2.imag) + (num1.imag * num2.real);
    return result;
}

// Function to display a complex number
void displayComplex(struct Complex num) {
    printf("%.2f + %.2fi\n", num.real, num.imag);
}

int main() {
    // Declare variables to store two complex numbers
    struct Complex complexNum1, complexNum2, sumResult, productResult;

    // Input details for the first complex number
    printf("Input details for Complex Number 1 (real imag): ");
    scanf("%f %f", &complexNum1.real, &complexNum1.imag);

    // Input details for the second complex number
    printf("Input details for Complex Number 2 (real imag): ");
    scanf("%f %f", &complexNum2.real, &complexNum2.imag);

    // Add the two complex numbers
    sumResult = addComplex(complexNum1, complexNum2);

    // Multiply the two complex numbers
    productResult = multiplyComplex(complexNum1, complexNum2);

    // Display the results
    printf("\nSum of Complex Numbers:\n");
    displayComplex(sumResult);

    printf("\nProduct of Complex Numbers:\n");
    displayComplex(productResult);

    return 0;
}

Output:

Input details for Complex Number 1 (real imag): 10 3
Input details for Complex Number 2 (real imag): 12 5

Sum of Complex Numbers:
22.00 + 8.00i

Product of Complex Numbers:
105.00 + 86.00i

Explanation:

In the exercise above,

  • The "Complex" structure is defined with members 'real' and 'imag'.
  • Functions like "addComplex()" and "multiplyComplex()" are implemented to perform addition and multiplication of two complex numbers, respectively.
  • The "displayComplex()" function is used to display a complex number.
  • The "main()" function prompts the user to input two complex numbers, then adds and multiplies them, and finally displays the results.

Flowchart:

Flowchart: C Program structure: Complex number operations.
Flowchart: C Program structure: Complex number operations.

For more Practice: Solve these Related Problems:

  • Write a C program to input two complex numbers and compute both their sum and difference.
  • Write a C program to multiply two complex numbers and display the result in the form a+bi.
  • Write a C program to divide one complex number by another and display the quotient in proper format.
  • Write a C program to input an array of complex numbers, compute their magnitudes, and then sort the complex numbers based on these magnitudes.

Go to:


PREV : Queue Implementation with Structures.
NEXT : C Program structure: Car rental cost calculation.

C Programming Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.