C Program To Subtract Two Complex Numbers Using Structures
In this tutorial, we are going to see how to write a C program to subtract 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 (2 + 3i) and (4 + 5i), then the output of the program will be (-2 -2i).
Write a C Program To Subtract Two Complex Numbers Using Structures
#include <stdio.h> struct complex { int r, i; }; int main() { struct complex a, b, c; 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); c.r = a.r - b.r; c.i = a.i - b.i; if (c.i >= 0) printf("Difference of the complex numbers = %d + %di", c.r, c.i); else printf("Difference of the complex numbers = %d %di", c.r, c.i); return 0; }
Output:
Enter the value a and b of the first complex number (a + ib): 2 3 Enter the value c and d of the second complex number (c + id): 4 5 Difference of the complex numbers = -2 -2i