Complex numbers are simply a combination of real and imaginary numbers. The follow the standard form of a+ib in which ‘a’ is the real part and ‘b’ is the imaginary part. During analysis of complex numbers, a number of operations are required to be performed such as addition, subtraction, multiplication etc. In this tutorial post, we’re going discuss a C Program to Add Two Complex Numbers.
The C program is presented here with description and sample output of the program. Given below is a simple and short algorithm to add two complex numbers. This algorithm can be used to write a program code in any programming language to add two complex numbers.
Algorithm:
- Start
- Declare and define structure to define the real and imaginary part of a complex number
- Declare complex number as structure data type
- Declare and define function to add the complex numbers
- Declare the variables in main() function
- Enter complex numbers to be added
- Call the function to add and perform addition operation
- Print the Result
Source Code to Add Two Complex Numbers in C:
#include<stdio.h>
struct com // defining structure
{
float r;// declaration of real part
float i;// declaration of imaginary part
};
struct com num1,num2; // declaration of complex numbers as structure data type
struct com add(struct com com1,struct com comp2) // function to add complex numbers
{
struct com temp;
temp.r = com1.r + comp2.r; // adding real part
temp.i = com1.i + comp2.i; // adding imaginary part
return temp;
}
int main()
{
struct com sum;
printf("\nThe Complex Number is of the standard form: a + ib\n");
printf("\nFirst Complex Number: ");
printf("\nEnter real and imaginary part:\n");
scanf("%f %f",&num1.r, &num1.i);
printf("\nSecond Complex Number: ");
printf("\nEnter real and imaginary part:\n");
scanf("%f%f",&num2.r,&num2.i);
sum = add(num1,num2);// calling function
printf("\nThe sum is %.2f + i%.2f\n\n", sum.r,sum.i);
return 0;
}
This program includes a single header file, stdio.h, for standard input/output functions such as prinff(), scanf() etc. As a single complex number has two parts: real and imaginary, it is necessary to define a structure to store the complex number. The program uses the structure struct com to define real and imaginary part as float data type.
During the addition of complex numbers, the real part should be added to real part and the imaginary part to imaginary part. So, in order to reduce the use of same line of code repeatedly, a user defined function is used here.
As the program is executed, it displays the standard format of complex number and asks for the complex numbers to be added. The complex numbers have been defined as structure data type. After entering both the complex numbers, the add() function is called with num1 and num2 as arguments.
In add() function, another variable, named temp, has been defined as a structure data type. It temporarily stores the sum of complex number and then returns to main() function. Finally, the result or the sum of two complex numbers is displayed as console output.
Also see,
C Program to Find Sum of First ‘N’ Natural Numbers
C Program to Solve Quadratic Equation
The complex numbers have a great significance in electrical circuit analysis, scientific research, mathematical study, etc. I hope this tutorial (source code and algorithm) was helpful to you. The source code is compiled in Code::Blocks IDE. If you have any queries regarding the program, mention them in the comments.
You can find more C tutorials here.