I'm new at C and I am working on a program to find the difference, product, and division of complex numbers. At the difference, it works but at the product and the division, the answer is 0+0i.
The code is:
#include <stdio.h>
struct complex {
float re; // The real part
float im; // The imaginary part
}complex1, complex2, sum;
// to find the difference
struct complex diff(struct complex z1, struct complex z2) {
// difference
sum.re = complex1.re - complex2.re;
sum.im = complex1.im - complex2.im;
//show the difference
printf("The difference is (%f, %fi)\n", sum.re, sum.im);
return sum;
}
// to find the product
struct complex product(struct complex z1, struct complex z2) {
// product
int x, y, a, b;
x = complex1.re * complex2.re;
y = complex1.im * complex2.im;
a = complex1.re * complex2.im;
b = complex2.re * complex1.im;
//show the product
printf("The product is (%f, %fi)\n", (x - y), (a + b));
return sum;
}
// to find the division
struct complex division(struct complex z1, struct complex z2) {
// division
int x, y, a, b;
x = (complex1.re * complex2.re) + (complex1.im * complex2.im);
y = (complex2.re * complex2.re) + (complex2.im * complex2.im);
a = (complex2.re * complex1.im) - (complex1.re * complex2.im);
b = (complex2.re * complex2.re) + (complex2.im * complex2.im);
//show the division
printf("The division is (%f, %fi)\n", (x / y), (a / b));
return sum;
}
int main(){
printf( "Enter the first complex number \n");
printf( "Enter the real part " ); scanf( "%f", &complex1.re );
printf( "Enter the imaginary part " ); scanf( "%f", &complex1.im );
printf( "Enter the second complex number \n");
printf( "Enter the real part " ); scanf( "%f", &complex2.re);
printf( "Enter the imaginary part " ); scanf( "%f", &complex2.im );
diff(complex1, complex2);
product(complex1, complex2);
division(complex1, complex2);
printf("\nThanks for using!!!!");
return 0;
}