Program8
8. Define a function that takes TWO objects representing complex numbers and returns a new complex number with the sum 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.
Objective of the Program
To understand class creation in Python
To create objects
To define methods
To perform object-based addition
To apply OOP concepts
Logical Construction (Think Before You Code)
A complex number has two parts:
a + bi
Where:
a= real partb= imaginary part
Step 1: Define Class
Class Complex must contain:
real part
imaginary part
method to display complex number
Step 2: Define Addition Function
If:
z1 = a + bi
z2 = c + di
Then:
z1 + z2 = (a + c) + (b + d)i
So we:
Add real parts
Add imaginary parts
Return new Complex object
Step 3: Add N Complex Numbers
Read N
Read N complex numbers
Store them in list
Initialize result as first number
Add remaining numbers one by one
👉 Key Idea:
Objects are being added — not just numbers.
Flowchart (Block Diagram of Logic)
Start
Read N
Create Complex objects
Store in list
Initialize result
Loop and add objects
Display final result
Stop
Algorithm
Start
Define class Complex
Define addition function
Read N
If N < 2 → Display error
For i in range N:
Read real part
Read imaginary part
Create object
Append to list
Initialize result = first object
For remaining objects:
result = add(result, next object)
Display result
Stop
Python Program
⚠ Students must complete missing parts.
# Program to add N complex numbers using class
class Complex:
def __init__(self, real, imag):
self.real = ____________
self.imag = ____________
def display(self):
print(self.real, "+", self.imag, "i")
def add_complex(c1, c2):
real_sum = c1.real + ____________
imag_sum = c1.imag + ____________
return Complex(real_sum, imag_sum)
n = int(input("Enter number of complex numbers (>=2): "))
complex_list = []
for i in range(n):
r = float(input("Enter real part: "))
im = float(input("Enter imaginary part: "))
complex_list.append(Complex(__________, ____________))
result = complex_list[0]
for i in range(1, n):
result = add_complex(result, ____________)
print("Sum of complex numbers:")
result.display()
Probable Output
Important Concepts Used
Class definition
Constructor (
__init__)Object creation
Object as function parameter
Return object
List of objects
OOP concepts
Viva Voce Questions
What is a class?
What is an object?
What is a constructor?
Why do we use
self?What is object passing?
Can we overload operators in Python?
What is difference between function and method?
Procedure to Execute the Program
Open Python IDE
Create new file
Type program
Save with
.pyextensionRun program
Enter values when prompted
Google Colab – Online Execution
👉 Paste your Colab link here.
[https://colab.research.google.com/drive/1EP2WorqKr0ic1rRWX866e8wkDcolDBQr?usp=sharing]
Program Explanation
Assignment for Students
Implement addition using
__add__methodSubtract complex numbers
Multiply complex numbers
Override
__str__method for better output
Lab Record Instructions
Right Side of the Record
1. Problem Statement
2. Python Program
Write the following neatly on the right-hand side page:
Left Side of the Record
Write the following neatly on the left-hand side page:
3. Flowchart
4. Algorithm
5. Output
⚠️ Students must write ALL possible outputs.

Comments
Post a Comment