P070.c (835B)
1 /* Write a program to perform addition of two complex number having fields 'real' and 'img' 2 of type integer. */ 3 4 #include <stdio.h> 5 6 struct complex 7 { 8 int real; 9 int img; 10 }; 11 12 void getData(struct complex *c) 13 { 14 printf("Enter real: "); 15 scanf("%d", &c->real); 16 printf("Enter imaginary: "); 17 scanf("%d", &c->img); 18 } 19 20 struct complex add(struct complex c1, struct complex c2) 21 { 22 struct complex r; 23 r.real = c1.real + c2.real; 24 r.img = c1.img + c2.img; 25 return r; 26 } 27 28 void display(struct complex c) 29 { 30 printf("%d+%di", c.real, c.img); 31 } 32 33 int main() 34 { 35 struct complex c1, c2, c3; 36 getData(&c1); 37 getData(&c2); 38 printf("\nComplex Number 1:\n"); 39 display(c1); 40 printf("\nComplex Number 2:\n"); 41 display(c2); 42 c3 = add(c1, c2); 43 printf("\nResult: \n"); 44 display(c3); 45 return 0; 46 }