Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct
- {
- double x;
- double y;
- }Complex;
- void input(Complex *num)
- {
- printf("Enter the real part ");
- scanf("%lf",&num->x);
- printf("Enter the imaginary part ");
- scanf("%lf",&num->y);
- }
- Complex add(Complex num1,Complex num2)
- {
- Complex sum;
- sum.x = num1.x + num2.x;
- sum.y = num1.y + num2.y;
- return sum;
- }
- Complex subtract(Complex num1,Complex num2)
- {
- Complex diff;
- diff.x = num1.x - num2.x;
- diff.y = num1.y - num2.y;
- return diff;
- }
- Complex multiply(Complex num1,Complex num2)
- {
- Complex product;
- product.x = (num1.x*num2.x) - (num1.y*num2.y);
- product.y = (num1.x*num2.y) + (num2.x*num1.y);
- return product;
- }
- Complex divide(Complex num1,Complex num2)
- {
- Complex div;
- div.x = ((num1.x*num2.x) + (num1.y*num2.y))/((num2.x*num2.x)+(num2.y*num2.y));
- div.y = ((num2.x*num1.y) - (num1.x*num2.y))/((num2.x*num2.x)+(num2.y*num2.y));
- return div;
- }
- void display(Complex num)
- {
- printf("%.2lf+i%.2lf \n",num.x,num.y);
- }
- int main()
- {
- Complex num1,num2;
- int choice;
- do
- {
- printf("Entering the first complex number \n");
- input(&num1);
- printf("Entering the second complex number \n");
- input(&num2);
- printf("Press 1 to add \n ");
- printf("Press 2 to subtract \n ");
- printf("Press 3 to multiply \n ");
- printf("Press 4 to divide \n ");
- printf("Press 5 to exit \n ");
- printf("Enter your choice [1-5] ");
- scanf("%d",&choice);
- switch(choice)
- {
- case 1:
- printf("The sum is ");
- display(add(num1,num2));
- break;
- case 2:
- printf("The difference is ");
- display(subtract(num1,num2));
- break;
- case 3:
- printf("The product is ");
- display(multiply(num1,num2));
- break;
- case 4:
- printf("The quotient is ");
- display(divide(num1,num2));
- break;
- case 5:
- break;
- default:
- printf("Bhag bc");
- }
- }
- while(choice!=5);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement