C

C Program To Divide Two Complex Numbers Using Structures

In this tutorial, we are going to see how to write a C program to divide two complex numbers using structures. The following C program will ask the user to enter two complex numbers and then display them. The user must enter the real and imaginary parts of the two complex numbers. For example, if a user enters two complex numbers as (1 + 2i) and (4 + 6i), then the output of the program will be (16/52 + 2/52i).
 


 

Write a C Program To Divide Two Complex Numbers Using Structures
#include <stdio.h>
 
struct complex
{
   int r, i;
};
 
int main()
{
   struct complex a, b, c;
   int x, y, z;

   printf("Enter the value a and b of the first complex number (a + ib): ");
   scanf("%d%d", &a.r, &a.i);
   printf("Enter the value c and d of the second complex number (c + id): ");
   scanf("%d%d", &b.r, &b.i);
 
   if (b.r != 0 && b.i != 0){

      x = a.r*b.r + a.i*b.i;
      y = a.i*b.r - a.r*b.i;
      z = b.r*b.r + b.i*b.i;

      if (x%z == 0 && y%z == 0)
      {
        if (y/z >= 0)
          printf("Division of the complex numbers = %d + %di", x/z, y/z);
        else
          printf("Division of the complex numbers = %d %di", x/z, y/z);
      }
      else if (x%z == 0 && y%z != 0)
      {
        if (y/z >= 0)
          printf("Division of two complex numbers = %d + %d/%di", x/z, y, z);
        else
          printf("Division of two complex numbers = %d %d/%di", x/z, y, z);
      }
      else if (x%z != 0 && y%z == 0)
      {
        if (y/z >= 0)
          printf("Division of two complex numbers = %d/%d + %di", x, z, y/z);
        else
          printf("Division of two complex numbers = %d %d/%di", x, z, y/z);
      }
      else
      {
        if (y/z >= 0)
          printf("Division of two complex numbers = %d/%d + %d/%di",x, z, y, z);
        else
          printf("Division of two complex numbers = %d/%d %d/%di", x, z, y, z);
      }
    }
    else
    {
		printf("Division by 0 + 0i is not allowed.");

    }
 
   return 0;
}

Output:

Enter the value a and b of the first complex number (a + ib): 1 2
Enter the value c and d of the second complex number (c + id): 4 6
Division of two complex numbers = 16/52 + 2/52i

 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *