/**************************************** * Section : 15 * Machine No. : N * Roll No. : 19CS100XY * Name : Aritra Hazra * Assignment No : 1 * Description : Complex Number Operations *****************************************/ #include #include int main () { double r1, r2, i1, i2, r, i; /* taking real and imaginary parts of a complex number from user as inputs */ printf("++ Enter Complex Number-1 ++\n"); printf("\t-- Real Part: "); scanf("%lf", &r1); printf("\t-- Imaginary Part: "); scanf("%lf", &i1); printf("++ Enter Complex Number-2 ++\n"); printf("\t-- Real Part: "); scanf("%lf", &r2); printf("\t-- Imaginary Part: "); scanf("%lf", &i2); /* printing the two complex numbers in (a+bi) format */ printf("\n** Complex Number-1: C1 = (%lf) + (%lf)i", r1, i1); printf("\n** Complex Number-2: C2 = (%lf) + (%lf)i\n", r2, i2); /* computing the modulus of two complex numbers */ printf("\n** Modulus of Complex Number-1: |C1| = %lf", sqrt(r1*r1+i1*i1)); printf("\n** Modulus of Complex Number-2: |C2| = %lf", sqrt(r2*r2+i2*i2)); /* computing the conjugate of two complex numbers */ printf("\n** Conjugate of Complex Number-1: ~C1 = (%lf) + (%lf)i", r1, -i1); printf("\n** Conjugate of Complex Number-2: ~C2 = (%lf) + (%lf)i", r2, -i2); /* computing the arguement of two complex numbers */ printf("\n** Argument of Complex Number-1: arg(C1) = %lf", atan(i1/r1)); printf("\n** Argument of Complex Number-2: arg(C2) = %lf", atan(i2/r2)); /* adding two complex numbers */ r = (r1+r2); i = (i1+i2); printf("\n** Complex Number after ADDITION: (C1 + C2) = (%lf) + (%lf)i", r, i); /* subtracting two complex numbers */ r = (r1-r2); i = (i1-i2); printf("\n** Complex Number after SUBTRACTION: (C1 - C2) = (%lf) + (%lf)i", r, i); /* multiplying two complex numbers */ r = (r1*r2-i1*i2); i = (r1*i2+r2*i1); printf("\n** Complex Number after MULTIPLICATION: (C1 x C2) = (%lf) + (%lf)i", r, i); /* dividing two complex numbers */ r = (r1*r2+i1*i2)/(r2*r2+i2*i2); i = (r2*i1-r1*i2)/(r2*r2+i2*i2); printf("\n** Complex Number after DIVISION: (C1 / C2) = (%lf) + (%lf)i", r, i); /* computing the squares of two complex numbers */ r = (r1*r1-i1*i1); i = 2*r1*i1; printf("\n** SQUARE of Complex Number-1: C1^2 = (%lf) + (%lf)i", r, i); r = (r2*r2-i2*i2); i = 2*r2*i2; printf("\n** SQUARE of Complex Number-2: C2^2 = (%lf) + (%lf)i", r, i); /* computing the cubes of two complex numbers */ r = (r1*r1*r1-3*r1*i1*i1); i = (3*r1*r1*i1-i1*i1*i1); printf("\n** CUBE of Complex Number-1: C1^3 = (%lf) + (%lf)i", r, i); r = (r2*r2*r2-3*r2*i2*i2); i = (3*r2*r2*i2-i2*i2*i2); printf("\n** CUBE of Complex Number-2: C2^3 = (%lf) + (%lf)i", r, i); /* computing the square-roots of two complex numbers */ r = sqrt((r1+sqrt(r1*r1+i1*i1))/2); i = ((i1>=0)-(i1<0))*sqrt((-r1+sqrt(r1*r1+i1*i1))/2); printf("\n** SQUARE ROOT of Complex Number-1: SQRT(C1) = (+/-) [(%lf) + (%lf)i]", r, i); r = sqrt((r2+sqrt(r2*r2+i2*i2))/2); i = ((i2>=0)-(i2<0))*sqrt((-r2+sqrt(r2*r2+i2*i2))/2); printf("\n** SQUARE ROOT of Complex Number-2: SQRT(C2) = (+/-) [(%lf) + (%lf)i]", r, i); /* computing the reciprocals of two complex numbers */ r = r1/(r1*r1+i1*i1); i = -i1/(r1*r1+i1*i1); printf("\n** RECIPROCAL of Complex Number-1: 1/C1 = (%lf) + (%lf)i", r, i); r = r2/(r2*r2+i2*i2); i = -i2/(r2*r2+i2*i2); printf("\n** RECIPROCAL of Complex Number-2: 1/C2 = (%lf) + (%lf)i", r, i); /* computing the exponentiation of two complex numbers (with respect to 'e') */ r = exp(r1)*cos(i1); i = exp(r1)*sin(i1); printf("\n** EXPONENTIATION of Complex Number-1: e^C1 = (%lf) + (%lf)i", r, i); r = exp(r2)*cos(i2); i = exp(r2)*sin(i2); printf("\n** EXPONENTIATION of Complex Number-2: e^C2 = (%lf) + (%lf)i\n", r, i); return (0); }