@@ -25,6 +25,10 @@ def multiplication():
2525def division ():
2626 n1 = float (input ("Enter first number: " ))
2727 n2 = float (input ("Enter second number: " ))
28+ if n2 == 0 :
29+ print ("Invalid entry" )
30+ return "Invalid entry"
31+ print (n1 / n2 )
2832
2933 return n1 / n2
3034
@@ -34,13 +38,70 @@ def average():
3438 return sum (nums ) / len (nums )
3539
3640
41+ def factorial (num ):
42+
43+ answer = 1
44+ for i in range (num ):
45+ answer *= i + 1
46+ return answer
47+
48+ def complexarithmetic ():
49+ print ("Enter '1' for complex addition" )
50+ print ("Enter '2' for complex substraction" )
51+ print ("Enter '3' for complex multiplication" )
52+ print ("Enter '4' for complex division" )
53+ choice = (input ("enter your choice" ))
54+ if choice == "1" :
55+ nums = list (map (int , input ("Enter all numbers seperated by space: " ).split ()))
56+ real_sum = 0
57+ imag_sum = 0
58+ for i in range (0 ,len (nums )- 1 ,2 ):
59+ real_sum += nums [i ]
60+ for i in range (2 ,len (nums )- 1 ,2 ):
61+ imag_sum += nums [i ]
62+ imag_sum += nums [- 1 ]
63+ return f"{ real_sum } + i{ imag_sum } "
64+
65+ elif choice == "2" :
66+ nums = list (map (int , input ("Enter all numbers seperated by space: " ).split ()))
67+ real_sub = nums [0 ]
68+ imag_sub = nums [1 ]
69+ for i in range (2 ,len (nums )- 1 ,2 ):
70+ real_sub -= nums [i ]
71+ for i in range (3 ,len (nums )- 1 ,2 ):
72+ imag_sub -= nums [i ]
73+ imag_sub -= nums [- 1 ]
74+ return f"{ real_sub } + i{ imag_sub } "
75+
76+ elif choice == "3" :
77+ nums = list (map (int , input ("Enter all numbers seperated by space maximum 4 elements: " ).split ()))
78+ real = (nums [0 ]* nums [2 ]- nums [1 ]* nums [3 ])
79+ imag = (nums [0 ]* nums [3 ]+ nums [2 ]* nums [1 ])
80+ return f"{ real } + i{ imag } "
81+
82+ elif choice == "4" :
83+ nums = list (map (int , input ("Enter all numbers seperated by space maximum 4 elements: " ).split ()))
84+ real = ((nums [0 ]* nums [2 ]+ nums [1 ]* nums [3 ])/ (nums [2 ]** 2 + nums [3 ]** 2 ))
85+ imag = ((nums [1 ]* nums [2 ] - nums [0 ]* nums [3 ])/ (nums [2 ]** 2 + nums [3 ]** 2 ))
86+ return f"{ real } + i{ imag } "
87+
88+ def binomail (num ):
89+ result = factorial (num [0 ])/ (factorial (num [1 ])* factorial (num [0 ]- num [1 ]))
90+ return result
91+
92+
93+
94+
3795c = 0
3896while c != "-1" :
3997 print ("Enter '1' for addition" )
4098 print ("Enter '2' for subtraction" )
4199 print ("Enter '3' for multiplication" )
42100 print ("Enter '4' for division" )
43101 print ("Enter '5' for average" )
102+ print ("Enter '6' for factorial" )
103+ print ("Enter '7' for complex arithmetic" )
104+ print ("ENter '8' for binomial" )
44105 print ("Enter '-1' to exit.\n " )
45106
46107 c = input ("Your choice is: " )
@@ -69,6 +130,8 @@ def average():
69130 elif c == "4" :
70131 res = division ()
71132 os .system ("cls" )
133+ if res == "Invalid entry" :
134+ continue
72135 print (f"The answer is { res } " )
73136 time .sleep (2 )
74137 os .system ("cls" )
@@ -80,12 +143,43 @@ def average():
80143 time .sleep (2 )
81144 os .system ("cls" )
82145
146+ elif c == "6" :
147+ num = int (input ("enter the number: " ))
148+ if num < 0 :
149+ print ("Invalid entry" )
150+ continue
151+ res = factorial (num )
152+ os .system ("cls" )
153+ print (f"The answer is { res } " )
154+ time .sleep (2 )
155+ os .system ("cls" )
156+
157+ elif c == "7" :
158+ os .system ("cls" )
159+ print (complexarithmetic ())
160+ time .sleep (2 )
161+ os .system ("cls" )
162+
163+ elif c == "8" :
164+ os .system ("cls" )
165+ num = list (map (int ,input ("Enter the number seperated by space" ).split ()))
166+ if num [0 ]< num [1 ]:
167+ print ("Invalid entry" )
168+ continue
169+ res = binomail (num )
170+ os .system ("cls" )
171+ print (f"The answer is { res } " )
172+ time .sleep (2 )
173+ os .system ("cls" )
174+
175+
83176 elif c == "-1" :
84177 os .system ("cls" )
85178 print ("Thank you for using the calculator!" )
86179 time .sleep (2 )
87180 break
88181
182+
89183 else :
90184 os .system ("cls" )
91185 print ("Sorry, invalid option!" )
0 commit comments