C Program to Add and Subtract two complex numbers using Structure


#include<stdio.h>

typedef struct complex
{
  int real;
  int imaginary;
}comp;

 
void Get_values(comp *s1,comp *s2);

void Calculate(comp *s1,comp *s2,comp *s3);
int main()
{
       comp s1,s2,s3;

       Get_values(&s1,&s2);
       Calculate(&s1,&s2,&s3);
       
       return 0;
}


void Get_values(comp *s1,comp *s2)
{
        printf("Enter the complex numbers of structure variable 1:\n");
	scanf("%d%d",&s1->real,&s1->imaginary);

	printf("complex number of s1: (%d)+(%d)i\n",s1->real,s1->imaginary);
        
	printf("Enter the complex numbers of structure variable 2:\n");
	scanf("%d%d",&s2->real,&s2->imaginary);

	printf("complex number of s2: (%d)+(%d)i\n",s2->real,s2->imaginary);
}

void Calculate(comp *s1,comp *s2,comp *s3)
{


        s3->real=s1->real + s2->real;
        s3->imaginary=s1->imaginary + s2->imaginary;
        printf("Addition of two complex numbers is (%d)+(%d)i\n",s3->real,s3->imaginary);
	     
	s3->real=s1->real - s2->real;
	s3->imaginary=s1->imaginary - s2->imaginary;
	printf("Subtraction  of two complex numbers is (%d)-(%d)i\n",s3->real,s3->imaginary);

}


Output:

Enter the complex numbers of structure variable 1:
4 5
complex number of s1: (4)+(5)i
Enter the complex numbers of structure variable 2:
3 4
complex number of s2: (3)+(4)i
Addition of two complex numbers is (7)+(9)i
Subtraction  of two complex numbers is (1)-(1)i



The above, Program is to Add and Subtract two complex numbers using structure.
Here, we have used two user-defined functions, the first one is to take the values as input which is the Get_values function and the second one is to Calculate the operation.