- 0
Notifications Mark All Read
No New notification
- Login
- Get Prime
C Program to add Complex Numbers
Program for adding two Complex Numbers
In this section, we will learn how to write a C program for adding two complex numbers in simple steps. Complex numbers are described as numbers which contain real as well as imaginary parts. Imaginary parts are identified by ‘ i ‘ symbol which is written along.
Adding Complex Numbers
Complex Number is defined as a combination of a real number and imaginary number. A complex number can be described as
A = x + yi
Here, x = real part
y = imaginary part
For representing a complex number using C program, a structure needs to be defined for representing both the real and imaginary parts.
C Program to add complex numbers:
#include<stdio.h> typedef struct complexNumber { int real; int img; } complex; complex add(complex x, complex y); int main() { complex a, b, sum; a.real = 3; a.img = 4; b.real = 5; b.img = 2; printf("\n a = %d + %di", a.real, a.img); printf("\n b = %d + %di", b.real, b.img); sum = add(a, b); printf("\n sum = %d + %di", sum.real, sum.img); return 0; } complex add(complex x, complex y) { complex add; add.real = x.real + y.real; add.img = x.img + y.img; return (add); }
Output:
a = 3 + 4i b = 5 + 2i sum = 8 + 6i
Explanation:
The above given C program works by defining a structure for storing a complex number. A complex add function is defined to add two complex numbers and then output the resulting complex number using the same structure.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment