The Wayback Machine - https://web.archive.org/web/20240721025434/https://www.geeksforgeeks.org/c-program-to-add-two-complex-numbers/
Open In App

C Program to Add Two Complex Numbers

Last Updated : 30 May, 2023
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

Complex numbers are those numbers that can be expressed in the form of “a+ib” where a and b are the real numbers and i is the imaginary part called “iota” The value of i is √-1. In this article, we are going to add two complex numbers using a C program.

Example of Add Two Complex Number

Input:
a = ( 2 + 3i )
b = ( 4 + 5i )

Output:
Sum = ( 6 + 8i )

Explanation:   

= ( 2 + 3i ) + ( 4 + 5i )
= ( 2 + 4 ) + ( 3 + 5 )i 
= ( 6 + 8i )

Approach

The approach is very simple for this program, follow the below steps. 

  1. Define a structure for complex numbers. 
  2. Define a function for adding two complex numbers. 
  3. This function accepts two complex numbers as parameters and returns a complex number.

Below is the C program to add two complex numbers:

C




// C program to demonstrate
// addition of complex numbers
#include <stdio.h>
 
// define a structure for complex number
typedef struct complexNumber {
    int real;
    int img;
} complex;
 
// This function accepts two complex numbers
// as parameter and return addition of
// them.
complex add(complex x, complex y);
 
// Driver code
int main()
{
    // Define three complex type numbers
    complex a, b, sum;
 
    // First complex number
    a.real = 2;
    a.img = 3;
 
    // Second complex number
    b.real = 4;
    b.img = 5;
 
    // Print first complex number
    printf("\n a = %d + %di", a.real, a.img);
 
    // print second complex number
    printf("\n b = %d + %di", b.real, b.img);
 
    // call add(a,b) function and
    // pass complex numbers a & b
    // as an parameter.
    sum = add(a, b);
 
    // Print result
    printf("\n sum = %d + %di", sum.real, sum.img);
 
    return 0;
}
 
// Complex add(complex x, complex y)
// function definition
complex add(complex x, complex y)
{
    // Define a new complex number.
    complex add;
 
    // Add real part of a&b;
    add.real = x.real + y.real;
 
    // Add Imaginary part of a&b;
    add.img = x.img + y.img;
 
    return (add);
}


Output

 a = 2 + 3i
 b = 4 + 5i
 sum = 6 + 8i

Explanation of the above method

Complex Numbers consist of real and img parts which will be declared as a struct complex. While In the function, int main() there are three instances of structs created a,b, and sum. And then values are assigned to both complex numbers. After assigning the values we are calling add function with a and b as a complex number. add function will return the addition of both objects as a sum.



Similar Reads

C Program To Add Two Numbers Represented By Linked Lists- Set 1
Given two numbers represented by two lists, write a function that returns the sum list. The sum list is a list representation of the addition of two input numbers. Example: Input: List1: 5-&gt;6-&gt;3 // represents number 563 List2: 8-&gt;4-&gt;2 // represents number 842 Output: Resultant list: 1-&gt;4-&gt;0-&gt;5 // represents number 1405 Explanat
4 min read
Add two numbers using ++ and/or --
Given two numbers, return a sum of them without using operators + and/or -, and using ++ and/or --.Examples: Input: x = 10, y = 5 Output: 15 Input: x = 10, y = -5 Output: 10 We strongly recommend you to minimize your browser and try this yourself first The idea is to do y times x++, if y is positive, and do y times x-- if y is negative. C/C++ Code
4 min read
&lt;complex.h&gt; header file in C with Examples
Most of the C Programs deals with complex number operations and manipulations by using complex.h header file. This header file was added in C99 Standard. C++ standard library has a header, which implements complex numbers as a template class, complex&lt;T&gt;, which is different from &lt;complex.h&gt; in C. Macros associated with &lt;complex.h&gt;
5 min read
LEX program to add line numbers to a given file
Lex is a computer program that generates lexical analyzers and was written by Mike Lesk and Eric Schmidt. Lex reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in the C programming language. Prerequisite: Flex (Fast lexical Analyzer Generator) Example: Input: #include&lt;stdio.h> // Driver Code int
2 min read
C Program to Add Two Integers
In this article, we will learn to write a C program for the addition of two integers. In C, the sum of two numbers can be done by the addition operator (+). This operator works by taking two operands and returning their sum as the result. C Program to Add Two NumbersBelow is the program to find the sum of two numbers in C (more specifically integer
2 min read
C Program to Multiply two Floating Point Numbers
Floating point numbers are a way to represent real numbers with both whole parts and fractional parts. In this article, we will learn how to write a C program to find the product of two floating-point numbers. The below image shows an example of floating point multiplication in C: C Program To Multiply Two Floating-Point Numbers The below C program
1 min read
C Program to Find Armstrong Numbers Between Two Integers
Prerequisite: Program for Armstrong Numbers A positive integer of n digits is called Armstrong number of order n (order is the number of digits) if abcd… = pow(a,n) + pow(b,n) + pow(c,n) + pow(d,n) + …. Here, we will build a C Program to print Armstrong Numbers between two integers. Example: 153 is an Armstrong number 1*1*1 + 5*5*5 + 3*3*3 = 153 37
3 min read
C Program to Display Prime Numbers Between Two Intervals Using Functions
Prime numbers have only 2 factors, 1 and themselves. For example, 2,3, 5, 7, 9,... are the first 5 prime numbers. Here we will build a C program to display prime numbers between two intervals using functions using 2 approaches, for loop and while loop. Example Input: num1 = 2, num2 = 10 Output: Prime numbers between 2 and 10 are: 2 3 5 7 Explanatio
3 min read
C Program For Adding Two Numbers Represented By Linked Lists- Set 2
Given two numbers represented by two linked lists, write a function that returns the sum list. The sum list is linked list representation of the addition of two input numbers. It is not allowed to modify the lists. Also, not allowed to use explicit extra space (Hint: Use Recursion). Example : Input: First List: 5-&gt;6-&gt;3 Second List: 8-&gt;4-
6 min read
C Program to Check Whether a Number Can Be Express as Sum of Two Prime Numbers
Prime numbers are numbers that have only 2 factors, 1 and themselves. For example, 2, 3, 5, 7, 11, etc are some of the first prime numbers. Here we will see whether a number can be expressed as the sum of two prime numbers using a C program. Example Input: 7Output: YesExplanation: 7 can be expressed as sum of 2 and 5 which are prime Input: 11Output
2 min read
three90RightbarBannerImg