c code for multiplication of two complex numbers










#include<stdio.h>
typedef struct COMPLEX{
    int a;
    int b;
}Complex;
Complex multiply(Complex,Complex);
int main(){
    int a1,b1,a2,b2;
    Complex x,y,z;
    printf("Enter first complex number : ");
    scanf("%d+%di",&a1,&b1);
    printf("\nEnter second complex number : ");
    scanf("%d+%di",&a2,&b2);
    x.a = a1; x.b = b1;
    y.a = a2; y.b = b2;
    z = multiply(x,y);
    printf("After multiplication: %d+%di",z.a,z.b);
    return 0;
}
Complex multiply(Complex x,Complex y){
    Complex z;
    z.a = x.a*y.a - x.b*y.b;
    z.b = x.a*y.b + x.b*y.a;
    return z;
}





Alogrithm:
**




4 comments:

Unknown said...

above code is incorrect. removind + and i from the scanf the code will work fine.
New and simple version for the above problem is
#include
int main()
{
int a,b,c,d,e,f;
printf("\nEnter the first complex number ");
scanf("%d %d",&a,&b);
printf("\nFirst number is ");
printf("%d + %di",a,b);
printf("\nEnter the first complex number ");
scanf("%d %d",&c,&d);
printf("\nSecond number is ");
printf("%d + %di",c,d);
e = a*c - b*d;
f = a*d + b*c;
printf("\nAfter multiplication ");
printf("%d+%di",e,f);
return 0;
}

praneetha choudhary said...

ya...it's very simple n easy 2 understand.....i have a doubt....for d above 1 ...while reading 1ly u gave dat format...in d similar way i also tried..but i got a garbage value...if v give like dat compiler will take garbage values na...how do i overcome dis problem...

Priyanka kumari said...

Hi parnaeeth
enter the number in that format. For example
5+10i

Unknown said...

#include
typedef struct COMPLEX{
int a;
int b;
}Complex;
Complex multiply(Complex,Complex);
int main()
{
int a1,b1,a2,b2;
Complex x,y,z;
printf("Enter real part of first complex number : ");
scanf("%d",&a1);
printf("Enter img part of first complex number : ");
scanf("%d",&b1);
printf("\nEnter real part of second complex number : ");
scanf("%d",&a2);
printf("\nEnter img part of second complex number : ");
scanf("%d",&b2);
x.a = a1; x.b = b1;
y.a = a2; y.b = b2;
z = multiply(x,y);
printf("\nAfter multiplication real part of new complex number: %d",z.a);
printf("\nAfter multiplication img part of new complex number: %d",z.b);
return 0;
}
Complex multiply(Complex x,Complex y)
{
Complex z;
z.a = x.a*y.a - x.b*y.b;
z.b = x.a*y.b + x.b*y.a;
return z;
}