You are here

C program to add, subtract, multiply and divide Complex Numbers, complex arithmetic

C program to add, subtract, multiply and divide complex numbers. It is a menu driven program in which a user will have to enter his/her choice to perform an operation and can perform operations as many times as required. To easily handle a complex number a structure named complex has been used, which consists of two integers, first integer is for real part of a complex number and second is for imaginary part.

C programming code

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct complex
  5. {
  6.   int real, img;
  7. };
  8.  
  9. int main()
  10. {
  11.   int choice, x, y, z;
  12.   struct complex a, b, c;
  13.  
  14.   while(1)
  15.   {
  16.     printf("Press 1 to add two complex numbers.\n");
  17.     printf("Press 2 to subtract two complex numbers.\n");
  18.     printf("Press 3 to multiply two complex numbers.\n");
  19.     printf("Press 4 to divide two complex numbers.\n");
  20.     printf("Press 5 to exit.\n");
  21.     printf("Enter your choice\n");
  22.     scanf("%d", &choice);
  23.  
  24.     if (choice == 5)
  25.       exit(0);
  26.  
  27.     if (choice >= 1 && choice <= 4)
  28.     {
  29.       printf("Enter a and b where a + ib is the first complex number.");
  30.       printf("\na = ");
  31.       scanf("%d", &a.real);
  32.       printf("b = ");
  33.       scanf("%d", &a.img);
  34.       printf("Enter c and d where c + id is the second complex number.");
  35.       printf("\nc = ");
  36.       scanf("%d", &b.real);
  37.       printf("d = ");
  38.       scanf("%d", &b.img);
  39.     }
  40.     if (choice == 1)
  41.     {
  42.       c.real = a.real + b.real;
  43.       c.img = a.img + b.img;
  44.  
  45.       if (c.img >= 0)
  46.         printf("Sum of the complex numbers = %d + %di", c.real, c.img);
  47.       else
  48.         printf("Sum of the complex numbers = %d %di", c.real, c.img);
  49.     }
  50.     else if (choice == 2)
  51.     {
  52.       c.real = a.real - b.real;
  53.       c.img = a.img - b.img;
  54.  
  55.       if (c.img >= 0)
  56.         printf("Difference of the complex numbers = %d + %di", c.real, c.img);
  57.       else
  58.         printf("Difference of the complex numbers = %d %di", c.real, c.img);
  59.     }
  60.     else if (choice == 3)
  61.     {
  62.       c.real = a.real*b.real - a.img*b.img;
  63.       c.img = a.img*b.real + a.real*b.img;
  64.  
  65.       if (c.img >= 0)
  66.         printf("Multiplication of the complex numbers = %d + %di", c.real, c.img);
  67.       else
  68.         printf("Multiplication of the complex numbers = %d %di", c.real, c.img);
  69.     }
  70.     else if (choice == 4)
  71.     {
  72.       if (b.real == 0 && b.img == 0)
  73.         printf("Division by 0 + 0i isn't allowed.");
  74.       else
  75.       {
  76.         x = a.real*b.real + a.img*b.img;
  77.         y = a.img*b.real - a.real*b.img;
  78.         z = b.real*b.real + b.img*b.img;
  79.  
  80.         if (x%z == 0 && y%z == 0)
  81.         {
  82.           if (y/z >= 0)
  83.             printf("Division of the complex numbers = %d + %di", x/z, y/z);
  84.           else
  85.             printf("Division of the complex numbers = %d %di", x/z, y/z);
  86.         }
  87.         else if (x%z == 0 && y%z != 0)
  88.         {
  89.           if (y/z >= 0)
  90.             printf("Division of two complex numbers = %d + %d/%di", x/z, y, z);
  91.           else
  92.             printf("Division of two complex numbers = %d %d/%di", x/z, y, z);
  93.         }
  94.         else if (x%z != 0 && y%z == 0)
  95.         {
  96.           if (y/z >= 0)
  97.             printf("Division of two complex numbers = %d/%d + %di", x, z, y/z);
  98.           else
  99.             printf("Division of two complex numbers = %d %d/%di", x, z, y/z);
  100.         }
  101.         else
  102.         {
  103.           if (y/z >= 0)
  104.             printf("Division of two complex numbers = %d/%d + %d/%di",x, z, y, z);
  105.           else
  106.             printf("Division of two complex numbers = %d/%d %d/%di", x, z, y, z);
  107.         }
  108.       }
  109.     }
  110.     else
  111.       printf("Invalid choice.");
  112.  
  113.     printf("\nPress any key to enter choice again...\n");
  114.   }
  115. }

Download Complex numbers program executable.

Complex arithmetic operations C program