- 1C Hello World Program
- 2C Program to Print Your Own Name
- 3C Program to Print an Integer Entered by the User
- 4C Program to Add Two Numbers
- 5C Program to Multiply Two Floating‑Point Numbers
- 6C Program to Print the ASCII Value of a Character
- 7C Program to Swap Two Numbers
- 8C Program to Calculate Fahrenheit to Celsius
- 9C Program to Find the Size of int, float, double and char
- 10C Program - Add Two Complex Numbers
- 11C Program - Find Simple Interest
- 12C Program - Find Compound Interest
- 13C Program - Area And Perimeter Of Rectangle
- 14C Program - Check Whether a Number is Positive, Negative, or Zero
- 15C Program - Check Whether Number is Even or Odd
- 16C Program - Check Whether a Character is Vowel or Consonant
- 17C Program - Find Largest Number Among Three Numbers
- 18C Program - Calculate Sum of Natural Numbers
- 19C Program - Print Alphabets From A to Z Using Loop
- 20C Program - Make a Simple Calculator
- 21C Program - Generate Multiplication Table
- 22C Program - Reverse a Number
- 23C Program - Check whether the input number is a Neon Number
- 24C Program - Find All Factors of a Natural Number
C Program – Add Two Complex Numbers - Struct & Separate Parts
Add Complex Numbers Using a Struct
#include <stdio.h>
typedef struct {
float real;
float imag;
} Complex;
int main() {
Complex a, b, sum;
printf("Enter real and imaginary parts of first complex number: ");
scanf("%f %f", &a.real, &a.imag);
printf("Enter real and imaginary parts of second complex number: ");
scanf("%f %f", &b.real, &b.imag);
sum.real = a.real + b.real;
sum.imag = a.imag + b.imag;
printf("Sum = %.1f + %.1fi\n", sum.real, sum.imag);
return 0;
}
Enter real and imaginary parts of first complex number: 3 2
Enter real and imaginary parts of second complex number: 1 7
Sum = 4.0 + 9.0i
This approach defines a struct to group the real and imaginary parts together. After reading both complex numbers, their respective parts are added and printed in standard form.
Add Complex Numbers Using Separate Variables
#include <stdio.h>
int main() {
float real1, imag1, real2, imag2, realSum, imagSum;
printf("Enter real and imaginary parts of first complex number: ");
scanf("%f %f", &real1, &imag1);
printf("Enter real and imaginary parts of second complex number: ");
scanf("%f %f", &real2, &imag2);
realSum = real1 + real2;
imagSum = imag1 + imag2;
printf("Sum = %.1f + %.1fi\n", realSum, imagSum);
return 0;
}
Enter real and imaginary parts of first complex number: 2 5
Enter real and imaginary parts of second complex number: 4 3
Sum = 6.0 + 8.0i
Instead of using a structure, this method handles each part individually. It works by storing and adding real parts separately from imaginary parts before displaying the combined result.
⬅ Previous TopicC Program to Find the Size of int, float, double and char
Next Topic ⮕C Program - Find Simple Interest
Next Topic ⮕C Program - Find Simple Interest
Comments
Loading comments...