I had to forcefully format the output in the required manner.
For example it had to be +i and not +1i ..
So not much logic in this problem except for the calculation.
========================================================================
#include<stdio.h>
void add(int,int,int,int);
void sub(int,int,int,int);
void mul(int,int,int,int);
int main()
{
int a,b,c,d;
scanf("%d\n%d\n%d\n%d",&a,&b,&c,&d);
add(a,b,c,d);
sub(a,b,c,d);
mul(a,b,c,d);
return 0;
}
void add(int a,int b,int c,int d)
{
if(b+d==1)
printf("%d+i\n",a+c);
else if(b+d==-1)
printf("%d-i\n",a+c);
else if(b+d>=0)
printf("%d+%di\n",a+c,b+d);
else
printf("%d%di\n",a+c,b+d);
}
void sub(int a,int b,int c,int d)
{
if(b-d==1)
printf("%d+i",a-c);
else if(b-d==-1)
printf("%d-i",a-c);
else if(b-d>=0)
printf("%d+%di\n",a-c,b-d);
else
printf("%d%di\n",a-c,b-d);
}
void mul(int a,int b,int c,int d)
{
if(a*d+b*c==1)
printf("%d+i",a*c-b*d);
else if(a*d+b*c==-1)
printf("%d-i",a*c-b*d);
else if(a*d+b*c>=0)
printf("%d+%di",a*c-b*d,a*d+b*c);
else
printf("%d%di",a*c-b*d,a*d+b*c);
}
For example it had to be +i and not +1i ..
So not much logic in this problem except for the calculation.
========================================================================
#include<stdio.h>
void add(int,int,int,int);
void sub(int,int,int,int);
void mul(int,int,int,int);
int main()
{
int a,b,c,d;
scanf("%d\n%d\n%d\n%d",&a,&b,&c,&d);
add(a,b,c,d);
sub(a,b,c,d);
mul(a,b,c,d);
return 0;
}
void add(int a,int b,int c,int d)
{
if(b+d==1)
printf("%d+i\n",a+c);
else if(b+d==-1)
printf("%d-i\n",a+c);
else if(b+d>=0)
printf("%d+%di\n",a+c,b+d);
else
printf("%d%di\n",a+c,b+d);
}
void sub(int a,int b,int c,int d)
{
if(b-d==1)
printf("%d+i",a-c);
else if(b-d==-1)
printf("%d-i",a-c);
else if(b-d>=0)
printf("%d+%di\n",a-c,b-d);
else
printf("%d%di\n",a-c,b-d);
}
void mul(int a,int b,int c,int d)
{
if(a*d+b*c==1)
printf("%d+i",a*c-b*d);
else if(a*d+b*c==-1)
printf("%d-i",a*c-b*d);
else if(a*d+b*c>=0)
printf("%d+%di",a*c-b*d,a*d+b*c);
else
printf("%d%di",a*c-b*d,a*d+b*c);
}
in the complex number code if the value with i is 0 than according to ur code it will print 0i
ReplyDeleteeg if the real value is 2 and imaginery value is 0 than according to ur code it will print 20i
but it is wrong