Get Answers to all your Questions

header-bg qa

Can any one help!!write a c program to find addition and subtraction of complex no:s

Answers (1)

best_answer

#include <stdio.h>
#include <stdlib.h>

 

struct complex

  int real, img;
;

int main()

  int choice, x, y, z;
  struct complex a, b, c;

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

    if (choice == 5)
      exit(0);

    if (choice >= 1 && choice <= 4)
    
      printf("Enter a and b where a + ib is the first complex number.");
      printf(" a = ");
      scanf("0", &a.real);
      printf("b = ");
      scanf("0", &a.img);
      printf("Enter c and d where c + id is the second complex number.");
      printf(" c = ");
      scanf("0", &b.real);
      printf("d = ");
      scanf("0", &b.img);
    
    if (choice == 1)
    
      c.real = a.real + b.real;
      c.img = a.img + b.img;

      if (c.img >= 0)
        printf("Sum of the complex numbers = 0 + 0i", c.real, c.img);
      else
        printf("Sum of the complex numbers = 0 0i", c.real, c.img);
    
    else if (choice == 2)
    
      c.real = a.real - b.real;
      c.img = a.img - b.img;

      if (c.img >= 0)
        printf("Difference of the complex numbers = 0 + 0i", c.real, c.img);
      else
        printf("Difference of the complex numbers = 0 0i", c.real, c.img);
    
    else if (choice == 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 the complex numbers = 0 + 0i", c.real, c.img);
      else
        printf("Multiplication of the complex numbers = 0 0i", c.real, c.img);
    
    else if (choice == 4)
    
      if (b.real == 0 && b.img == 0)
        printf("Division by 0 + 0i isn't allowed.");
      else
      
        x = a.real*b.real + a.img*b.img;
        y = a.img*b.real - a.real*b.img;
        z = b.real*b.real + b.img*b.img;

        if (x%z == 0 && y%z == 0)
        
          if (y/z >= 0)
            printf("Division of the complex numbers = 0 + 0i", x/z, y/z);
          else
            printf("Division of the complex numbers = 0 0i", x/z, y/z);
        
        else if (x%z == 0 && y%z != 0)
        
          if (y/z >= 0)
            printf("Division of two complex numbers = 0 + 0/0i", x/z, y, z);
          else
            printf("Division of two complex numbers = 0 0/0i", x/z, y, z);
        
        else if (x%z != 0 && y%z == 0)
        
          if (y/z >= 0)
            printf("Division of two complex numbers = 0/0 + 0i", x, z, y/z);
          else
            printf("Division of two complex numbers = 0 0/0i", x, z, y/z);
        
        else
        
          if (y/z >= 0)
            printf("Division of two complex numbers = 0/0 + 0/0i",x, z, y, z);
          else
            printf("Division of two complex numbers = 0/0 0/0i", x, z, y, z);
        
      
    
    else
      printf("Invalid choice.");

    printf(" Press any key to enter choice again... ");
  

Posted by

Deependra Verma

View full answer