### Solutions for Skillrack CSE1002 Inlab 2

Total time : 20 mins

Challenges : 1

### QuestionÂ (Complex Number)

A computer scientist working in image processing is working on discrete Fourier transform. He needs an implementation of complex number to use in his program. Develop an algorithm and write a C program to implement addition, subtraction and multiplication in complex numbers. Implement each operation as a function and call it in your main. The function call sequence is addition, subtraction and multiplication. For example when the complex numbers are 3+2i, 1+7i the output should be

4+9i

2-5i

-11+23i

**Input Format**

Real part of complex number1

Imaginary part of complex number1

Real part of complex number2

Imaginary part of complex number2

**Output Format**

Resultant complex number represented as

real part +/- imaginary part followed by an ‘i’

### Solution

#include< stdio.h > void add(int r1,int i1,int r2,int i2) { if(i1+i2 > 0) printf("%d+%di\n",r1+r2,i1+i2); else printf("%d%di\n",r1+r2,i1+i2); } void multi(int r1,int i1,int r2,int i2) { int d=(r1*i2)+(r2*i1); if(d > 0) printf("%d+%di",(r1*r2)-(i1*i2),d); else if(d==-1) printf("%d-i",(r1*r2)-(i1*i2)); else printf("%d%di",(r1*r2)-(i1*i2),d); } void main() { int r1,r2,i1,i2; scanf("%d%d%d%d",&r1,&i1,&r2,&i2); add(r1,i1,r2,i2); add(r1,i1,-r2,-i2); multi(r1,i1,r2,i2); }

### Input

INPUT: 3 2 1 7

### Output

OUTPUT: 4+9i 2-5i -11+23i