Write a C program that uses functions to perform the following operations:

  1. Reading a complex number
  2. Writing a complex number
  3. Addition of two complex numbers
  4. Multiplication of two complex numbers

(Note: represent complex number using a structure.)

Algorithm:

Step 1: Start
Step 2: declare structure for complex numbers
Step 3: read the complex number
Step 4: read choice
Step 5: if choice = 1 then addition operation will perform and it contains following steps
            i) w.realpart = w1.realpart + w2.realpart;
             ii) w.imgpart = w1.imgpart + w2.imgpart; goto step 4
Step 6: if choice = 2 then multiplication operation will perform and it contains following        steps
            i) w.realpart = (w1.realpart * w2.realpart)-(w1.imgpart * w2.imgpart);
            ii) w.imgpart = (w1.realpart * w2.imgpart)+(w1.imgpart * w2.realpart); goto step 4
Step 7: if choice = 0 then exit operation will perform
Step 8: if w.imgpart > 0 then print realpart+imgpart else Print realpart.
Step 9: Stop

Program:

#include <stdio.h>
#include <conio.h>
struct complex
{
   float real, imag;
}a, b, c;
   struct complex read(void);
   void write(struct complex);
   struct complex add(struct complex, struct complex);
   struct complex sub(struct complex, struct complex);
   struct complex mul(struct complex, struct complex);
   struct complex div(struct complex, struct complex);
void main ()
{
   clrscr();
   printf("Enter the 1st complex number\n ");
   a = read();
   write(a);
   printf("Enter  the 2nd complex number\n");
   b = read();
   write(b);
   printf("Addition\n ");
   c = add(a, b);
   write(c);
   printf("Substraction\n ");
   c = sub(a, b);
   write(c);
   printf("Multiplication\n");
   c = mul(a, b);
   write(c);
   printf("Division\n");
   c = div(a, b);
  write(c);
  getch();
}
struct complex read(void)
{
   struct complex t;
   printf("Enter the real part\n");
   scanf("%f", &t.real);
   printf("Enter the imaginary part\n");
   scanf("%f", &t.imag);
   return t;
}
void write(struct complex a)
{
   printf("Complex number  is\n");
   printf(" %.1f + i %.1f", a.real, a.imag);
   printf("\n");
}
struct complex add(struct complex p, struct complex q)
{
   struct complex t;
   t.real = (p.real + q.real);
   t.imag = (p.imag + q.imag);
   return t;
}
struct complex sub(struct complex p, struct complex q)
{
   struct complex t;
   t.real = (p.real - q.real);
   t.imag = (p.imag - q.imag);
   return t;
}
struct complex mul(struct complex p, struct complex q)
{
   struct complex t;
   t.real=(p.real * q.real) - (p.imag * q.imag);
   t.imag=(p.real * q.imag) + (p.imag * q.real);
   return t;
}
struct complex div(struct complex p, struct complex q)
{
   struct complex t;
   t.real = ((p.imag * q.real) - (p.real * q.imag)) / ((q.real * q.real) + (q.imag * q.imag));
   t.imag = ((p.real * q.real) + (p.imag * q.imag)) / ((q.real * q.real) + (q.imag * q.imag));
   return(t);
}

Input & Output:

Enter the real part
2
Enter the imaginary part 
4
Complex number is
2.0 + i4.0
Enter the real part 
4
Enter the imaginary part
2
Complex number is
4.0 + i2.0
Addition
Complex number is 
6.0 + i6.0
Subtraction
Complex number is 
-2.0 + i2.0
Multiplication
Complex number is 
0.0 + i20.0
Division
Complex number is
0.6 + i0.8