Q:

C program to calculate the addition of two complex numbers

0

C program to calculate the addition of two complex numbers

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

complex number contains two parts real and imaginary.

Read two complex numbers from the user, and then calculate the addition of both complex numbers and print the result.

Program:

The source code to calculate the addition of two complex numbers is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.

// C program to calculate the addition of
// two complex numbers

#include <stdio.h>

typedef struct
    {
    int real;
    int img;
} Complex;

int main()
{
    Complex num1;
    Complex num2;
    Complex num3;

    printf("Enter a first complex number (real and imaginary): ");
    scanf("%d%d", &num1.real, &num1.img);

    printf("Enter a second complex number (real and imaginary): ");
    scanf("%d%d", &num2.real, &num2.img);

    num3.real = num1.real + num2.real;
    num3.img = num1.img + num2.img;

    if (num3.img >= 0)
        printf("Result is = %d + %di\n", num3.real, num3.img);
    else
        printf("Result is = %d %di\n", num3.real, num3.img);

    return 0;
}

Output:

RUN 1:
Enter a first complex number (real and imaginary): 10 11
Enter a second complex number (real and imaginary): 12 23
Result is = 22 + 34i

RUN 2:
Enter a first complex number (real and imaginary): 1 2
Enter a second complex number (real and imaginary): 3 0
Result is = 4 + 2i

Explanation:

Here, we created a structure Complex that contains real and imaginary parts. And, in the main() function, we read the value of two complex numbers and calculated the addition of both numbers, and printed the result.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C programming Basic Input, Output, if else, Ternary Operator based Programs

Similar questions


need a help?


find thousands of online teachers now
C program to extract the last two digits from a gi... >>
<< C program to calculate the product of two binary n...