C Program to Add Two Complex Numbers

«
»

This is a C Program to add two complex numbers.

Problem Description

This C Program adds two complex numbers.

Problem Solution

A complex number is a number that can be put in the form a + bi, where a and b are real numbers and i is called the imaginary unit, where i2 = -1. In this expression, a is called the real part and b the imaginary part of the complex number.

Program/Source Code

Here is source code of the C Program to add two complex numbers. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * C Program to Add two Complex Numbers
 */
#include <stdio.h>
 
struct complex
{
   int realpart, imaginary;
};
 
main()
{
    struct complex a, b, c;
 
    printf("Enter value of a and b complex number a + ib.\n");
    printf("value of complex number a is = ");
    scanf("%d", &a.realpart);
    printf("value of complex number b is = ");
    scanf("%d", &a.imaginary);
    printf("Enter value of c and d complex number c + id.\n");
    printf("value of complex number c is = ");
    scanf("%d", &b.realpart);
    printf("value of complex number d is = ");
    scanf("%d", &b.imaginary);
    c.realpart = a.realpart + b.realpart;
    c.imaginary = a.imaginary + b.imaginary;
    if (c.imaginary >= 0)
        printf("complex numbers sum is = %d + %di\n", c.realpart, c.imaginary);
    else
        printf("complex numbers sum = %d %di\n", c.realpart, c.imaginary);
    return 0;
}
Program Explanation

In this C program, we are reading the value for complex number using the ‘realpart’ and ‘imaginary’ variables respectively. A complex number is a number that can be put in the form a + bi, where ‘a’ and’ b’ are real numbers and ‘i’ is called the imaginary unit, where i2 = -1. In this expression, ‘a’ is called the real part and ‘b’ the imaginary part of the complex number.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement

The variable ‘a’ and’ b’ are the objects of struct complex and it is used to access the ‘realpart’ and ‘imaginary’ variables in struct complex. The ‘c.realpart’ variable is used to add the value of a.realpart and b.realpart variables and ‘c.imaginary’ variable is used to add the value of a.imaginary and b.imaginary variables. If-else condition statement is used to check the value of c.imaginary variable is greater than or equal to 0, if the condition is true then it will execute the statement and print the value of addition of two complex numbers.

Runtime Test Cases
 
$ cc pgm55.c
$ a.out
Enter value of a and b complex number a + ib.
value of complex number a is = 10
value of complex number b is = 12
Enter value of c and d complex number c + id.
value of complex number c is = 15
value of complex number d is = 22
complex numbers sum is = 25 + 34i

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Simple C Programs, go to Simple C Programs. If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.