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

Answers (1)

#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... ");
  

Most Viewed Questions

Preparation Products

Knockout JEE Main April 2021 (One Month)

Personalized AI Tutor and Adaptive Time Table, Self Study Material, Weekend Live Classes, Mentorship from our Experts, Unlimited Mock Tests and Personalized Analysis Reports, 24x7 Doubt Chat Support,.

₹ 14000/- ₹ 4999/-
Buy Now
Knockout NEET Aug 2021 (One Month)

Personalized AI Tutor and Adaptive Time Table, Self Study Material, Weekend Live Classes, Mentorship from our Experts, Unlimited Mock Tests and Personalized Analysis Reports, 24x7 Doubt Chat Support,.

₹ 14000/- ₹ 4999/-
Buy Now
Knockout JEE Main May 2021

Personalized AI Tutor and Adaptive Time Table, Self Study Material, Weekend Live Classes, Mentorship from our Experts, Unlimited Mock Tests and Personalized Analysis Reports, 24x7 Doubt Chat Support,.

₹ 22999/- ₹ 9999/-
Buy Now
Knockout NEET Aug 2021

Personalized AI Tutor and Adaptive Time Table, Self Study Material, Weekend Live Classes, Mentorship from our Experts, Unlimited Mock Tests and Personalized Analysis Reports, 24x7 Doubt Chat Support,.

₹ 22999/- ₹ 9999/-
Buy Now
Test Series JEE Main May 2021

Unlimited Chapter Wise Tests, Unlimited Subject Wise Tests, Unlimited Full Mock Tests, Get Personalized Performance Analysis Report,.

₹ 6999/- ₹ 2999/-
Buy Now
Boost your Preparation for JEE Main with our Foundation Course
 
Exams
Articles
Questions