######## OOPS PRACTICE ###### Complex Number Class

 Problem Statement

Suggest Edit

A ComplexNumber class contains two data members: one is the real part (R) and the other is imaginary (I) (both integers).

Implement the Complex numbers class that contains the following functions -

1. Constructor

You need to create the appropriate constructor.

2. Plus -

This function adds two given complex numbers and updates the first complex number.

e.g.

if C1 = 4 + i5 and C2 = 3 +i1
C1.plus(C2) results in: 
C1 = 7 + i6 and C2 = 3 + i1
3. Multiply -

This function multiplies two given complex numbers and updates the first complex number.

e.g.

if C1 = 4 + i5 and C2 = 1 + i2
C1.multiply(C2) results in: 
C1 = -6 + i13 and C2 = 1 + i2
4. Print -

This function prints the given complex number in the following format :

a + ib

Note: There is space before and after '+' (plus sign) and no space between 'i' (iota symbol) and b.

Input Format :
The first line of the input contains two integers - real and imaginary part of 1st complex number.

The second line of the input contains Two integers - the real and imaginary part of the 2nd complex number.

The first line of the input contains an integer representing choice (1 or 2) (1 represents plus function will be called and 2 represents multiply function will be called)
Output Format :
The only line of the output prints the complex number in the following format a + ib
Sample Input 1 :
4 5
6 7
1
Sample Output 1 :
10 + i12
Sample Input 2 :
4 5
6 7
2
Sample Output 2 :
-11 + i58


class ComplexNumbers:
    
    #create your class here.
    def __init__(self,real,img):
        self.real=real
        self.img=img
    def plus(self,c2):
        self.real+=c2.real
        self.img+=c2.img
    def printer(self):
        print(str(self.real)+' + i'+str(self.img))

    def multiply(self,c2):
        temp_real=self.real
        temp_img=self.img
        self.real = (temp_real*c2.real)-(temp_img*c2.img)
        self.img = (temp_real*c2.img)+(temp_img*c2.real)
        

a,b=input().split()
c,d=input().split()
e=int(input())

c1=ComplexNumbers(int(a),int(b))
c2=ComplexNumbers(int(c),int(d))
if e==1:
    c1.plus(c2)
    c1.printer()
else:
    c1.multiply(c2)
    c1.printer()
    
  
    





    
    
    
    
    
    
#Driver's code goes here.
    

    
    
    
    
    
    
    
    
    
    


Comments

Popular posts from this blog

Sum of Even Numbers till N

Find the Runner-Up Score!

Print All Substrings