C Program to Add Two Complex Number Using Structure, Typedef & Pointer
This C program adds two complex number given by user using the concept of structure, typedef and pointer.
C Source Code: Addition of Two Complex Number
#include<stdio.h>
/* Declaring Structure */
typedef struct
{
float real;
float imaginary;
}complex;
/* Function Prototype */
complex add(complex *p, complex *q);
int main()
{
/* Declaring structure variable using struct complex */
complex cnum1, cnum2, mul;
printf("Enter real and imaginary part of first complex number:\n");
scanf("%f%f", &cnum1.real, &cnum1.imaginary);
printf("Enter real and imaginary part of second complex number:\n");
scanf("%f%f", &cnum2.real, &cnum2.imaginary);
mul = add(&cnum1, &cnum2);
printf("SUM = %0.2f + i %0.2f", mul.real, mul.imaginary);
return 0;
}
/* Function Definition */
complex add(complex *p, complex *q)
{
complex temp;
temp.real = p->real + q->real;
temp.imaginary = p->real + q->imaginary;
return temp;
}
To understand the working of structure and pointer, we encourage you to read code explanation for how to multiply two complex number using structure, typedef and pointer?
Output
Enter real and imaginary part of first complex number: 1 2 Enter real and imaginary part of second complex number: 2 3 SUM = 3.00 + i 4.00