编写程序以使用C将两个复数相加
问题
如何添加两个复数,这些复数由用户使用C程序在运行时输入-
解决方案
复数是可以是实部和虚部的组合的数字。
它以a+ib的形式表示。
程序
例如,在将两个复数相加后,我们将其取为(4+2i)和(5+3i),结果为9+5i。
#include输出结果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; }
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)