BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

C Program to multiply two complex numbers using structure

Last Updated: May 22, 2024 by Chaitanya Singh | Filed Under: C Programs

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:

  1. Prompt user to enter real and imaginary part of first complex number. The user inputs 3 and 2.
  2. Prompt user to enter real and imaginary part of second complex number. The user inputs 1 and 7.
  3. 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
  4. The result -11.00 + 23.00i is 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 multiplyComplex takes two struct Complex arguments 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 three Complex variables.
  • printf and scanf are used to take input from the user and store the entered complex numbers in structures c1 and c2.
  • 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.

Top Related Articles:

  1. C Program to Store Information of Students Using Structure
  2. C Program to Add two numbers
  3. C Program for employee salary calculation using structure
  4. C Program to read and print employee details using structure
  5. C Program to concatenate two strings without using strcat

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap