c code for complex numbers

  • cstructurecomplex number
  • write a c code for complex numbers using structures

    1. Define a structure “complex” (typedef) to read two complex numbers and perform addition, and subtraction of these two complex numbers, and display the result.

    Code

    # include<stdio.h> typedef struct complex{ int a; int b; }cmp; void display(cmp *x){ printf("%d + %di\n",x->a,x->b); } void add(cmp *x,cmp *y){ printf("%d + %di\n",x->a+y->a,x->b+y->b); } void sub(cmp *x,cmp *y){ if(x->b>y->b){ printf("%d %di\n",x->a-y->a,x->b-y->b); } printf("%d + %di\n",x->a-y->a,y->b-x->b); } int main(){ cmp c1,c2; c1.a=2; c1.b=3; c2.a=5; c2.b=6; display(&c1); display(&c2); add(&c1,&c2); sub(&c1,&c2); return 0; }