C program to add, subtract and multiply of Complex Numbers - IProgramX

Q. Accept two complex numbers from the user (real part, imaginary part). Write a menu driven program to perform the following operations till the user selects Exit.
i. ADD ii. SUBTRACT iii. MULTIPLY iv. EXIT



Program

#include <stdio.h>
#include <stdlib.h>
int main()
{
  int choice, temp1, temp2, temp3;
  int a_real, b_real, c_real , a_img, b_img, c_img;

  while(1)
  {
    printf("\nPress 1 to add two complex numbers.\n");
    printf("Press 2 to subtract two complex numbers.\n");
    printf("Press 3 to multiply two complex numbers.\n");
    printf("Press 4 to divide two complex numbers.\n");
    printf("Press 5 to exit.\n");
    printf("Enter your choice\n");
    scanf("%d",&choice);

    if( choice == 5)
      exit(0);
       printf("Enter a and b where a + ib is the first complex number.");
      printf("\na = ");
       scanf("%d", &a_real);
      printf("b = ");
      scanf("%d", &a_img);
        printf("Enter c and d where c + id is the second complex number.");
        printf("\nc = ");
      scanf("%d", &b_real);
        printf("d = ");
       scanf("%d", &b_img);
    switch (choice)
{
    case 1:
    {
      c_real = a_real + b_real;
      c_img = a_img + b_img;

      if ( c_img >= 0 )
        printf("Sum of two complex numbers = %d + %di",c_real,c_img);
      else
        printf("Sum of two complex numbers = %d %di",c_real,c_img);
         break;
    }
    case 2:
    {
      c_real = a_real - b_real;
      c_img = a_img - b_img;

      if ( c_img >= 0 )
        printf("Difference of two complex numbers = %d + %di",c_real,c_img);
      else
        printf("Difference of two complex numbers = %d %di",c_real,c_img);
            break;
    }
    case 3:
    {
      c_real = a_real*b_real - a_img*b_img;
      c_img = a_img*b_real + a_real*b_img;

      if ( c_img >= 0 )
        printf("Multiplication of two complex numbers = %d + %di",c_real,c_img);
      else
        printf("Multiplication of two complex numbers = %d %di",c_real,c_img);
            break;
    }
    case 4:
    {
      if ( b_real == 0 && b_img == 0 )
        printf("Division by 0 + 0i isn't allowed.");
      else
      {
        temp1 = a_real*b_real + a_img*b_img;
        temp2 = a_img*b_real - a_real*b_img;
        temp3 = b_real*b_real + b_img*b_img;

        if ( temp1%temp3 == 0 && temp2%temp3 == 0 )
        {
          if ( temp2/temp3 >= 0)
            printf("Division of two complex numbers = %d + %di",temp1/temp3,temp2/temp3);
          else
            printf("Division of two complex numbers = %d %di",temp1/temp3,temp2/temp3);
        }
        else if ( temp1%temp3 == 0 && temp2%temp3 != 0 )
        {
          if ( temp2/temp3 >= 0)
            printf("Division of two complex numbers = %d + %d/%di",temp1/temp3,temp2,temp3);
          else
            printf("Division of two complex numbers = %d %d/%di",temp1/temp3,temp2,temp3);
        }
        else if ( temp1%temp3 != 0 && temp2%temp3 == 0 )
        {
          if ( temp2/temp3 >= 0)
            printf("Division of two complex numbers = %d/%d + %di",temp1,temp3,temp2/temp3);
          else
            printf("Division of two complex numbers = %d %d/%di",temp1,temp3,temp2/temp3);
        }
        else
        {
          if ( temp2/temp3 >= 0)
            printf("Division of two complex numbers = %d/%d + %d/%di",temp1,temp3,temp2,temp3);
          else
            printf("Division of two complex numbers = %d/%d %d/%di",temp1,temp3,temp2,temp3);
        }
      }
          break;
    }
    default:printf("Invalid choice.");
            printf("\nPress any key to enter choice again...\n");
   }
 }
}

Output:

Press 1 to add two complex numbers.
Press 2 to subtract two complex numbers.
Press 3 to multiply two complex numbers.
Press 4 to divide two complex numbers.
Press 5 to exit.
Enter your choice
3
Enter a and b where a + ib is the first complex number.
a = 2
b = 5
Enter c and d where c + id is the second complex number.
c = 4
d = 8
Multiplication of two complex numbers = -32 + 36i
Press 1 to add two complex numbers.
Press 2 to subtract two complex numbers.
Press 3 to multiply two complex numbers.
Press 4 to divide two complex numbers.
Press 5 to exit.

Enter your choice

Post a Comment

0 Comments