VOOZH about

URL: https://www.geeksforgeeks.org/c/c-program-to-add-two-complex-numbers/

⇱ C Program to Add Two Complex Numbers - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

C Program to Add Two Complex Numbers

Last Updated : 30 May, 2023

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:


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.

Comment
Article Tags: