Write a program to add two complex numbers using C


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
");    scanf("%d%d", &x.realnumber, &x.imaginarynumber);    printf("enter second complex number z and p
");    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)
", 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)

Updated on: 05-Mar-2021

278 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements