C Program to Perform Complex Number Multiplication

This C program performs complex number multiplication. This program generally demonstrates the arithmatic operations on complex numbers.

Here is the source code of the C program to multiply two complex numbers. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to perform complex number multiplication
  3.  */
  4. #include<stdio.h>
  5. typedef struct COMPLEX{
  6.     int a;
  7.     int b;
  8. }Complex;
  9. Complex multiply(Complex, Complex);
  10. int main(){
  11.     int a1, b1, a2, b2;
  12.     Complex x, y, z;
  13.     printf("Enter first complex number : ");
  14.     scanf("%d+%di", &a1, &b1);
  15.     printf("\nEnter second complex number : ");
  16.     scanf("%d+%di", &a2, &b2);
  17.     x.a = a1;
  18.     x.b = b1;
  19.     y.a = a2; 
  20.     y.b = b2;
  21.     z = multiply(x, y);
  22.     printf("\nAfter multiplication: %d+%di", z.a, z.b);
  23.     return 0;
  24. }
  25. Complex multiply(Complex x, Complex y){
  26.     Complex z;
  27.     z.a = x.a * y.a - x.b * y.b;
  28.     z.b = x.a * y.b + x.b * y.a;
  29.     return z;
  30. }

$ gcc complex.c -o complex
$ ./complex
 
Enter first complex number : 2+1i 
Enter second complex number : 2+1i 
After multiplication: 3+4i

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.