top of page

Inlab 2

Complex numbers(Id-1054)

 

Question:

              A computer Scientist working in image processing is working on discrete Fourier transform.He need an implementation of complex number to use in his program. Develop an algorithm and write a program to implement addotion ,subtraction and multiplication in complex numbers. Implement each operation as a function and call it in your main. The function call sequence is addtion,subtraction and multiplication. For example when the complex number are 3+2i, 1+7i then 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’

OUTPUT
INPUT

Read  Real part of complex number1 as 'r1'

             Imaginary part of complex number1 as 'r2'

             Real part of complex number2 as 'i1'

             Imaginary part of complex number2 as 'i2'

Display Resultant complex number               represented as

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

PROCESSING

Declaring variables r1,r2,i1,i2

Read r1,r2,i1,i2

Call function addition ((r1+r2),(i1+i2))

Call function addition((r1-r2),(i1-i2))

Call function multiplication()

addition()

check whether b equals 1

display a+i

check whether b equals -1

display a-i

check whether b is greater 0

display a b

check whether b is less than 0

display a b

multiplication()

d equals (r1*i2)+(i1*r2);

x equals (r1*r2)-(i1*i2);

check whether d==1

display x

otherwise check whether d equals -1

display x

C  PROGRAM

#include<stdio.h>

void main()

{

int r1,r2,i1,i2;

void addition(int a,int b)

{

if(b==1)

printf("%d+i \n",a);

else if(b==-1)

printf("%d-i \n",a);

else if(b>0)

printf("%d+%di \n",a,b);

else if(b<0)

printf("%d%di \n",a,b);

}

void multiplication()

{

int d=(r1*i2)+(i1*r2);

int x=(r1*r2)-(i1*i2);

if(d==1)

printf("%d+i \n",x);

else if(d==-1)printf("%d-i \n",x);

else if(d>0)

printf("%d+%di \n",x,d);

else printf("%d%di \n",x,d);

 

}

scanf("%d%d%d%d",&r1,&i1,&r2,&i2);

addition(r1+r2,i1+i2);

addition(r1-r2,i1-i2);

multiplication();

}

bottom of page