In this tutorial, we will learn how to write a C program to multiply two complex numbers using structure.
C Program to find the product of two complex numbers using structure
In this program, user is asked to enter two complex numbers, the program then calculates the product of entered numbers. The explanation of the program is at the end of this code.
#include <stdio.h>
// Structure to store a complex number. A complex number
// contains real and imaginary part, for example, in number 2+3i
// the real part is 2 and imaginary part is 3
struct Complex {
// To hold real part of the complex number
float real;
// To hold imaginary part of the complex number
float imag;
};
// Function to multiply two complex numbers
struct Complex multiplyComplex(struct Complex c1, struct Complex c2) {
struct Complex result;
// Calculate the real part of the output complex number
result.real = c1.real * c2.real - c1.imag * c2.imag;
// Calculate the imaginary part of the output complex number
result.imag = c1.real * c2.imag + c1.imag * c2.real;
// Return the complex number which represents the product of
// entered complex numbers
return result;
}
int main() {
struct Complex c1, c2, result;
// Prompt user to enter the first complex number
printf("Enter real and imaginary parts of the first complex number: ");
scanf("%f %f", &c1.real, &c1.imag);
// Prompt user to enter the second complex number
printf("Enter real and imaginary parts of the second complex number: ");
scanf("%f %f", &c2.real, &c2.imag);
// Call function to multiply the entered numbers
result = multiplyComplex(c1, c2);
// Print the output complex number that represents product
printf("Product of the complex numbers: %.2f + %.2fi\n", result.real, result.imag);
return 0;
}
Output:
Enter real and imaginary parts of the first complex number: 3 2
Enter real and imaginary parts of the second complex number: 1 7
Product of the complex numbers: -11.00 + 23.00i
Explanation of output:
- Prompt user to enter real and imaginary part of first complex number. The user inputs
3and2. - Prompt user to enter real and imaginary part of second complex number. The user inputs
1and7. - The program calculates the product of the complex numbers:
- Real part:
(3 * 1 - 2 * 7) = 3 - 14 = -11 - Imaginary part:
(3 * 7 + 2 * 1) = 21 + 2 = 23
- Real part:
- The result
-11.00 + 23.00iis printed.
How this program works?
1. Include Standard Input-Output Library:
#include <stdio.h>
It is included so that we can use printf and scanf functions.
2. Define the Structure:
struct Complex {
float real; // Real part of the complex number
float imag; // Imaginary part of the complex number
};
The struct Complex is defined to store a complex number with real and imag parts. For example, In the complex number 3 + 4i, the real part is 3 and imaginary part is 4.
3. Function to Multiply Complex Numbers:
struct Complex multiplyComplex(struct Complex c1, struct Complex c2) {
struct Complex result;
// Calculate the real part of the product
result.real = c1.real * c2.real - c1.imag * c2.imag;
// Calculate the imaginary part of the product
result.imag = c1.real * c2.imag + c1.imag * c2.real;
return result; // Return the resulting complex number
}
- The function
multiplyComplextakes twostruct Complexarguments and returns their product. - The real part of the result is calculated as
(c1.real * c2.real - c1.imag * c2.imag). - The imaginary part of the result is calculated as
(c1.real * c2.imag + c1.imag * c2.real).
4. Main Function:
struct Complex c1, c2, result;declares threeComplexvariables.printfandscanfare used to take input from the user and store the entered complex numbers in structuresc1andc2.result = multiplyComplex(c1, c2);calls the function to multiply the complex numbers.printf("Product of the complex numbers: %.2f + %.2fi\n", result.real, result.imag);prints the result.
Leave a Reply