Skip to content
Blog

Add Two Complex Numbers in C

By Published A short read

     Hi everyone in this article we are writing a C Program to add two complex numbers. Before we start make sure that you are familiar with complex numbers and how the arithmetic operations work on the complex numbers.

    So now let's start the program.

    code:

#include<stdio.h>
#include<conio.h>
struct complex{
    int real, img;
};
int main(){
    struct complex a, b, c;
    printf("Enter a and b where a + ib is the first complex number. \n");
    printf("a = ");
    scanf("%d", &a.real);
    printf("b = ");
    scanf("%d", &a.img);
    printf("Enter c and d where c + id is the second complex number. \n");
    printf("c = ");
    scanf("%d", &b.real);
    printf("d = ");
    scanf("%d", &b.img);
    c.real = a.real + b.real;
    c.img = a.img + b.img;
    if(c.img >= 0)
        printf("Sum of two complex numbers is %d + %di \n", c.real, c.img);
    else
        printf("Sum of two complex numbers is %d - %di \n", c.real, -c.img);
    getch();
    return 0;
}

    output:

When the executable asks for the input if the input is 

    a = 3

    b = 4

    c = 2

    d = 1

then output will be = 5 + 5i

complex numbers addition
Output - Addition of two numbers