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 part

  • b = 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

  1. Read N

  2. Read N complex numbers

  3. Store them in list

  4. Initialize result as first number

  5. Add remaining numbers one by one

👉 Key Idea:
Objects are being added — not just numbers.


Flowchart (Block Diagram of Logic)


Flowchart Explanation
  • Start

  • Read N

  • Create Complex objects

  • Store in list

  • Initialize result

  • Loop and add objects

  • Display final result

  • Stop


Algorithm

  1. Start

  2. Define class Complex

  3. Define addition function

  4. Read N

  5. If N < 2 → Display error

  6. For i in range N:

    • Read real part

    • Read imaginary part

    • Create object

    • Append to list

  7. Initialize result = first object

  8. For remaining objects:

    • result = add(result, next object)

  9. Display result

  10. 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

Enter number of complex numbers (>=2): 3
Enter real part: 2
Enter imaginary part: 3
Enter real part: 4
Enter imaginary part: 5
Enter real part: 1
Enter imaginary part: 2

Sum of complex numbers:
7.0 + 10.0 i

Important Concepts Used

  • Class definition

  • Constructor (__init__)

  • Object creation

  • Object as function parameter

  • Return object

  • List of objects

  • OOP concepts


Viva Voce Questions

  1. What is a class?

  2. What is an object?

  3. What is a constructor?

  4. Why do we use self?

  5. What is object passing?

  6. Can we overload operators in Python?

  7. What is difference between function and method?



Procedure to Execute the Program

  1. Open Python IDE

  2. Create new file

  3. Type program

  4. Save with .py extension

  5. Run program

  6. 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__ method

  • Subtract 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.



-:END:


Comments

Popular posts from this blog