Write a program to add two complex numbers using C

CServer Side ProgrammingProgramming

Problem

How to add two complex numbers which are entered at run time by the user using C program −

Solution

A complex number is a number that can be a combination of real and imaginary parts.

It is represented in the form of a+ib.

Program

For example, let’s take the two complex numbers as (4+2i) and (5+3i) after adding the two complex numbers, the result is 9+5i.

 Live Demo

#include <stdio.h>
struct complexNumber{
   int realnumber, imaginarynumber;
};
int main(){
   struct complexNumber x, y, z,p;
   printf("enter first complex number x and y\n");
   scanf("%d%d", &x.realnumber, &x.imaginarynumber);
   printf("enter second complex number z and p\n");
   scanf("%d%d", &y.realnumber, &y.imaginarynumber);
   z.realnumber =x.realnumber + y.realnumber;
   z.imaginarynumber =x.imaginarynumber +y.imaginarynumber;
   printf("Sum of the complex numbers: (%d) + (%di)\n", z.realnumber, z.imaginarynumber);
   return 0;
}

Output

Enter first complex number x and y.
2 3
Enter second complex number z and p.
4 5
Sum of the complex numbers: (6) + (8i)
raja
Published on 05-Mar-2021 10:37:48
Advertisements