-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomplexnum2.c
127 lines (114 loc) · 3.63 KB
/
complexnum2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <stdio.h>
#include <stdlib.h>
struct complex
{
int real, img;
};
int main()
{
int choice, x, y, z;
struct complex a, b, c;
while(1)
{
printf("Press 1 to add two complex numbers.\n");
printf("Press 2 to subtract two complex numbers.\n");
printf("Press 3 to multiply two complex numbers.\n");
printf("Press 4 to divide two complex numbers.\n");
printf("Press 5 to exit.\n");
printf("Enter your choice\n");
scanf("%d", &choice);
if (choice == 5)
exit(0);
if (choice >= 1 && choice <= 4)
{
printf("Enter a and b where a + ib is the first complex number.");
printf("\na = ");
scanf("%d", &a.real);
printf("b = ");
scanf("%d", &a.img);
printf("Enter c and d where c + id is the second complex number.");
printf("\nc = ");
scanf("%d", &b.real);
printf("d = ");
scanf("%d", &b.img);
}
if (choice == 1)
{
c.real = a.real + b.real;
c.img = a.img + b.img;
if (c.img >= 0)
printf("Sum of the complex numbers = %d + %di", c.real, c.img);
else
printf("Sum of the complex numbers = %d %di", c.real, c.img);
}
else if (choice == 2)
{
c.real = a.real - b.real;
c.img = a.img - b.img;
if (c.img >= 0)
printf("Difference of the complex numbers = %d + %di", c.real, c.img);
else
printf("Difference of the complex numbers = %d %di", c.real, c.img);
}
else if (choice == 3)
{
c.real = a.real*b.real - a.img*b.img;
c.img = a.img*b.real + a.real*b.img;
if (c.img >= 0)
printf("Multiplication of the complex numbers = %d + %di", c.real, c.img);
else
printf("Multiplication of the complex numbers = %d %di", c.real, c.img);
}
else if (choice == 4)
{
if (b.real == 0 && b.img == 0)
printf("Division by 0 + 0i isn't allowed.");
else
{
x = a.real*b.real + a.img*b.img;
y = a.img*b.real - a.real*b.img;
z = b.real*b.real + b.img*b.img;
if (x%z == 0 && y%z == 0)
{
if (y/z >= 0)
printf("Division of the complex numbers = %d + %di", x/z, y/z);
else
printf("Division of the complex numbers = %d %di", x/z, y/z);
}
else if (x%z == 0 && y%z != 0)
{
if (y/z >= 0)
printf("Division of two complex numbers = %d + %d/%di", x/z, y, z);
else
printf("Division of two complex numbers = %d %d/%di", x/z, y, z);
}
else if (x%z != 0 && y%z == 0)
{
if (y/z >= 0)
printf("Division of two complex numbers = %d/%d + %di", x, z, y/z);
else
printf("Division of two complex numbers = %d %d/%di", x, z, y/z);
}
else
{
if (y/z >= 0)
printf("Division of two complex numbers = %d/%d + %d/%di",x, z, y, z);
else
printf("Division of two complex numbers = %d/%d %d/%di", x, z, y, z);
}
}
}
else
printf("Invalid choice.");
printf("\nPress any key to enter choice again...\n");
}
}
/*
👋 Hi, I’m @aarushinair - Aarushi Nair (she/her/ella)
👀 I’m a Computer Science Engineering Student
💞️ I’m looking to collaborate on #java, #python, #R, #applicationdevelopment
🌱 #GirlsWhoCode #WomenInTech #WomenInIT #WomenInSTEM #CyberSecurity #QuantumComputing #BlockChain #AI #ML
📫 How to reach me: https://www.linkedin.com/in/aarushinair/
👩🏫 YouTube Channel - Code with Aarushi : https://www.youtube.com/channel/UCKj5T1ELHCmkGKujkpqtl7Q
🙋 Follow me on Twitter: https://twitter.com/aarushinair_
*/