C

Write a C Program To Add Two Complex Numbers Using Structures

In this tutorial, we are going to see how to write a C program to add 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 (5 + 8i).
 

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

   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);
 
   sum.r = a.r + b.r;
   sum.i = a.i + b.i;
 
   printf("Sum of the complex numbers: %d + %di\n", sum.r, sum.i);
 
   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
Sum of the complex numbers: 5 + 8i

 

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 *