C Program to Add Two Complex Number Using Structure & Pointer

This C program adds two complex number given by user using the concept of structure and pointer.

C Source Code: Addition of Two Complex Number


#include<stdio.h>

/* Declaring Structure */
struct complex
{
    float real;
    float imaginary;
};

/* Function Prototype */
struct complex add(struct complex *p, struct complex *q);

int main()
{
    /* Declaring structure variable using struct complex */
    struct 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 */
struct complex add(struct complex *p, struct complex *q)
{
    struct complex temp;
    temp.real = p->real + q->real;
    temp.imaginary = p->real + q->imaginary;
    return temp;
}

Output

Enter real and imaginary part of first complex number:
1
2
Enter real and imaginary part of second complex number:
4
5
SUM = 5.00 + i 6.00