Pages

Friday, March 13, 2009

Dealing With Complex Numbers

Complex numbers was my favourite lesson of GCE A/L Mathematics, although I horribly screwed up the question under it, during the A/L examinaion. I was startled by finding out that calculations of complex numbers can be done with C programming. Here is my first complex number program.
#include "stdio.h"
#include "complex.h"
#include "stdlib.h"
int main()
{
double x1,x2,y1,y2;
char x[3],w[3],y[3],k[3];
printf("This is a complex number program\n");
printf("Enter your numbers in x+iy format\n\n");
printf("Enter real part of the #1, RZ1 :");
gets(x);
x1=atof(x);
printf("Enter imaginary part of the #1, IZ1 :");
gets(w);
y1=atof(w);
printf("\nZ1= %.2f+%.2fi\n\n",x1,y1);
double complex z1=x1 + y1*I;
printf("Enter real part of the #2, RZ2 :");
gets(y);
x2=atof(y);
printf("Enter imaginary part of the #2, IZ2 :");
gets(k);
y2=atof(k);
printf("\nZ2= %.2f+%.2fi\n\n",x2,y2);
double complex z2=x2 + y2*I;
double complex sum=z1+z2;
printf("Sum of the numbers(Z1+Z2) :%7.2f%+5.2fi\n",creal(sum),cimag(sum));
double complex dif=z1-z2;
printf("Difference of the numbers(Z1-Z2) :%7.2f%+5.2fi\n",creal(dif),cimag(dif));
double complex pro=z1*z2;
printf("Product of the numbers(Z1XZ2) :%7.2f%+5.2fi\n",creal(pro),cimag(pro));
double complex quo=z1/z2;
printf("Quotient of the numbers(Z1/Z2) :%7.2f%+5.2fi\n",creal(quo),cimag(quo));
double complex con1=conj(z1);
double complex con2=conj(z2);
printf("Conjugate of Z1:%7.2f%+5.2fi\nConjugate of Z2:%7.2f%+5.2fi\n\n",creal(con1)
,cimag(con1),creal(con2),cimag(con2));
printf("(Z1+Z2)(Z1-Z2) =%.2f%+.2fi\n",creal(sum*dif),cimag(sum*dif));
printf("(x1+iy1)(x1-iy1)=x1%c+y1%c= %.2f%+.2fi\n",253,253,creal(z1*con1),cimag(z1*con1));
printf("(x2+iy2)(x2-iy2)=x2%c+y2%c= %.2f%+.2fi\n",253,253,creal(z2*con2),cimag(z2*con2));
getchar();
return 0;
}

0 comments:

Post a Comment

මේ ලිපිය ගැන ඔබට හිතෙන්නේ මොකක්ද? ගල්, මුල්, මල් ඕන දේකට ඉඩ...
සිංහලෙන් යතුරු කරන්නට යුනිකේත එසැණින් පරිවර්තකය භාවිතා කරන්න පුළුවන්.