I have an assignment in which i have to add, subtract, multiply and divide two complex numbers and i am very confused as to how to create the program.
This is what i have got so far..

#include <stdio.h>
#include <complex.h>

int main (void)

int real, imag;

{
    complex a, b, c, d;

    printf("Please enter the two real and imaginary complex numbers (a+jb) :\n")
    printf("a = \n");
    scanf("%d", &a.real);
    printf("b = \n");
    scanf("%d", &b.imag);

    printf("Please enter the next two real and imaginary complex numbers (c+jd) :\n")
    printf("c = \n");
    scanf("%d", &c.real);
    printf("d = \\n");
    scanf("%d", &d.imag);

}

obviously i don't know if this is right but i am hoping so, now i don't know how to write the expression for adding the two complex numbers or outputting the end value.

Recommended Answers

All 11 Replies

you can make struct called complex
used to represent complex number

#include<stdio.h> 
#include<stdlib.h>

struct complex
{
  int real,imag;

};

int main()
{
     struct complex x,y;

    printf("Please enter the two real and imaginary complex numbers (a+jb) :\n");
    printf("real number:");
    scanf("%d",&x.real);
    printf("\nImaginary number:");
    scanf("%d", &x.imag);
    printf("Please enter the next two real and imaginary complex numbers (c+jd) :\n");
    printf("\nreal number:");
    scanf("%d",&y.real);
    printf("\nImaginary number:");
    scanf("%d",&y.imag);

      printf("\nFrist Complex Number:%d+i%d",x.real,x.imag);
      printf("\nSecond Complex Number:%d+i%d",y.real,y.imag);


    return 0;
} 
#include<stdio.h> 
#include<stdlib.h>
#include<conio.h>
#include<math.h>

struct Complex
{
  int real,imag;

};

struct Complex Add_Comp(struct Complex e1,struct Complex e2); // add two complex number
struct Complex Mul_Comp(struct Complex e1,struct Complex e2); // multiply two complex number
struct Complex Sub_Comp(struct Complex e1,struct Complex e2); // subtract two complex number


int main()
{
     struct Complex x,y,z;
     clrscr();

    printf("Please enter the two real and imaginary complex numbers (a+jb) :\n");
    printf("real number:");
    scanf("%d",&x.real);
    printf("\nImaginary number:");
    scanf("%d", &x.imag);
    printf("Please enter the next two real and imaginary complex numbers (c+jd) :\n");
    printf("\nreal number:");
    scanf("%d",&y.real);
    printf("\nImaginary number:");
    scanf("%d",&y.imag);



     printf("\nFrist Complex Number:%d+i%d",x.real,x.imag);
     printf("\nSecond Complex Number:%d+i%d",y.real,y.imag);

      z=Add_Comp(x,y);
      printf("\nResult of Add two Complex Number:%d+i%d",z.real,z.imag);
      z=Mul_Comp(x,y);
      printf("\nResult of Multiply two Complex Number:%d+i%d",z.real,z.imag);
      z=Sub_Comp(x,y);
       if(z.real==0)
       {
          printf("\nResult of Subtract two Complex Number:i%d",z.imag);
       }
       else if(z.imag==0)
       {
          printf("\nResult of Subtract two Complex Number:%d",z.real);
       }
       else
       {
          printf("\nResult of Subtract two Complex Number:%d+i%d",z.real,z.imag);
       }


    getch();
    return 0;
}

struct Complex Add_Comp(struct Complex e1,struct Complex e2)
{

  struct Complex e3;

  e3.real=e1.real+e2.real;
  e3.imag=e1.imag+e2.imag;

   return e3;
}

struct Complex Mul_Comp(struct Complex e1,struct Complex e2)
{

    struct Complex e3;

  e3.real=e1.real*e2.real;
  e3.imag=e1.imag*e2.imag;

   return e3;

}

struct Complex Sub_Comp(struct Complex e1,struct Complex e2)
{
     struct Complex e3;

  e3.real=abs(e1.real-e2.real);
  e3.imag=abs(e1.imag-e2.imag);

   return e3;

}

this code add and multiply and subtract two complex number

i'm wanting to do separate programs for each (aka one program for adding one for subtracting) is this possible?

yes you just take only function which make add two complex number in one program

this program will be for adding

like that::

#include<stdio.h> 
#include<stdlib.h>

struct Complex
{
  int real,imag;
};

struct Complex Add_Comp(struct Complex e1,struct Complex e2); // add two complex number

int main()
{
     struct Complex x,y,z;

    printf("Please enter the two real and imaginary complex numbers (a+jb) :\n");
    printf("real number:");
    scanf("%d",&x.real);
    printf("\nImaginary number:");
    scanf("%d", &x.imag);
    printf("Please enter the next two real and imaginary complex numbers (c+jd) :\n");
    printf("\nreal number:");
    scanf("%d",&y.real);
    printf("\nImaginary number:");
    scanf("%d",&y.imag);
     printf("\nFrist Complex Number:%d+i%d",x.real,x.imag);
     printf("\nSecond Complex Number:%d+i%d",y.real,y.imag);
      z=Add_Comp(x,y);
      printf("\nResult of Add two Complex Number:%d+i%d",z.real,z.imag);

    return 0;
}
struct Complex Add_Comp(struct Complex e1,struct Complex e2)
{
  struct Complex e3;
  e3.real=e1.real+e2.real;
  e3.imag=e1.imag+e2.imag;
   return e3;
}

i'm wanting to do separate programs for each (aka one program for adding one for subtracting) is this possible?

Yes.

OMFG!!!!! thank you SO much...i am actually going to cry i've been trying to do this the past 3 days haaaa!! :)

i have to make the multiplication and division in polar form but i'll try and figure it out on my own :')

you just write conditions of polar form like that:

struct Complex e1;
int degree,r;

 if(e1.real==0)
 {

    degree=atan(e1.imag/e1.real);   // degree tan-1(imag/real) 

 }
 else
 {
   degree=0;

 }

 r=sqrt((e1.real*e1.real)+(e1.imag*e1.imag));  // r=squre root of(squre(real)+squre(imag))

by this code you have polar form (r,degree) for complex number

 z=r*cos(degree)+i r*sin(degree)

sorry correct code is :

struct Complex e1;
int degree,r;
 if(e1.real!=0)   // real not equal zero
 {
    degree=atan(e1.imag/e1.real);   // degree tan-1(imag/real) 
 }
 else
 {
   degree=0;
 }
 r=sqrt((e1.real*e1.real)+(e1.imag*e1.imag));  // r=squre root of(squre(real)+squre(imag))

okay so i was wrong, i have to do a menu driven program...basically i have to press 1 to add two complex numbers press 2 to subtract, press 3 to multiply and 4 to divide

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.