MCPcopy Create free account
hub / github.com/Mrinank-Bhowmick/python-beginner-projects / complex_arithmetic

Function complex_arithmetic

projects/Calculator/main.py:68–126  ·  view source on GitHub ↗

Function to execute complex arithmetic operations such as addition, subtraction, multiplication, and division. Asks the user to choose the operation and input the complex numbers as real and imaginary parts, performs the operation, and returns the result.

()

Source from the content-addressed store, hash-verified

66
67
68def complex_arithmetic():
69 """
70 Function to execute complex arithmetic operations such as addition, subtraction, multiplication, and division.
71
72 Asks the user to choose the operation and input the complex numbers as real and imaginary parts,
73 performs the operation, and returns the result.
74 """
75 print("Enter '1' for complex addition")
76 print("Enter '2' for complex subtraction")
77 print("Enter '3' for complex multiplication")
78 print("Enter '4' for complex division")
79 choice = input("enter your choice")
80 if choice == "1":
81 nums = list(map(int, input("Enter all numbers separated by space: ").split()))
82 real_sum = 0
83 imag_sum = 0
84 for i in range(0, len(nums) - 1, 2):
85 real_sum += nums[i]
86 for i in range(2, len(nums) - 1, 2):
87 imag_sum += nums[i]
88 imag_sum += nums[-1]
89 return f"{real_sum}+ i{imag_sum}"
90
91 elif choice == "2":
92 nums = list(map(int, input("Enter all numbers separated by space: ").split()))
93 real_sub = nums[0]
94 imag_sub = nums[1]
95 for i in range(2, len(nums) - 1, 2):
96 real_sub -= nums[i]
97 for i in range(3, len(nums) - 1, 2):
98 imag_sub -= nums[i]
99 imag_sub -= nums[-1]
100 return f"{real_sub}+ i{imag_sub}"
101
102 elif choice == "3":
103 nums = list(
104 map(
105 int,
106 input(
107 "Enter all numbers separated by space maximum 4 elements: "
108 ).split(),
109 )
110 )
111 real = nums[0] * nums[2] - nums[1] * nums[3]
112 imag = nums[0] * nums[3] + nums[2] * nums[1]
113 return f"{real}+ i{imag}"
114
115 elif choice == "4":
116 nums = list(
117 map(
118 int,
119 input(
120 "Enter all numbers separated by space maximum 4 elements: "
121 ).split(),
122 )
123 )
124 real = (nums[0] * nums[2] + nums[1] * nums[3]) / (nums[2] ** 2 + nums[3] ** 2)
125 imag = (nums[1] * nums[2] - nums[0] * nums[3]) / (nums[2] ** 2 + nums[3] ** 2)

Callers 1

main.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected