Python lab program 9
Python lab program 9
Define a function which takes TWO objects representing complex numbers and
returns new complex number with a addition of two complex numbers. Define a
suitable class ‘Complex’ to represent the complex number. Develop a program to
read N (N >=2) complex numbers and to compute the addition of N complex
numbers.
class Complex:
def initComplex(self):
self.realPart = int(input("Enter the Real Part: "))
self.imgPart = int(input("Enter the Imaginary Part: "))
def display(self):
print(self.realPart, "+", self.imgPart, "i", sep="")
• initComplex(self):
• Ask the user to enter the real part and imaginary part.
• Store these values in self.realPart and self.imgPart.
• display(self):
• Print the complex number in the format: real part + imaginary part i.
• sum(self, c1, c2):
8. End
Output:
Enter first complex number
Enter the Real Part: 3
Enter the Imaginary Part: 4
First Complex Number: 3+4i
Enter second complex number
Enter the Real Part: 2
Enter the Imaginary Part: 3
Second Complex Number: 2+3i
Sum of two complex numbers is 5+7i