ปัญหา
วิธีบวกจำนวนเชิงซ้อนสองตัวที่ผู้ใช้ป้อนในขณะใช้งานโดยใช้โปรแกรม C -
วิธีแก้ปัญหา
จำนวนเชิงซ้อนคือจำนวนที่สามารถผสมกันระหว่างส่วนจริงและส่วนจินตภาพได้
โดยจะแสดงในรูปของ a+ib
โปรแกรม
ตัวอย่างเช่น ลองหาจำนวนเชิงซ้อนสองตัวเป็น (4+2i) และ (5+3i) หลังจากบวกจำนวนเชิงซ้อนสองตัวแล้ว ผลลัพธ์คือ 9+5i
#include <stdio.h>
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)