Complex Number Problem Solved: Coding Ninjas

First Index Of a Number in an Array

Complex Number problems are basically asked in many coding questions. This question is taken from one of the basic challenges in Coding Ninjas.

Before starting the actual question, let us know more about the complex numbers for a better understanding of the concepts.

Complex numbers are the numbers that are expressed in the form of a+ib where, a,b are real numbers and  ‘i’ is an imaginary number called “iota”. The value of i = (√-1). For example, 4+9i is a complex number, where 4 is a real number (Re) and 9i is an imaginary number (Im). 

Power of i

The alphabet i is referred to as the iota and is subsidiary to represent the imaginary part of the complex number. Further the iota(i) is very subsidiary to find the square root of negative numbers. We have the value of i2 = -1, and this is utilized to find the value of √-4 = √i24 = +2i The value of i2 = -1 is the fundamental aspect of an complex number. Let us endeavor and understand more about the incrementing powers of i.

i = √-1
i2 = -1
i3 = i.i2 = i(-1) = -i
i4 = (i2)2 = (-1)2 = 1
i4n = 1
i4n + 1 = I

The below question simply shows the addition and multiplication of complex numbers.

Problem :

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 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 :
Line 1 : Two integers - real and imaginary part of 1st complex number
Line 2 : Two integers - real and imaginary part of 2nd complex number
Line 3 : An integer representing choice (1 or 2) (1 represents plus function will be called and 2 represents multiply function will be called)
Output format :
Check details of 'print' function given above.
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

Solution :

 * Following is the main function we are using internally.
 * Refer this for completing the ComplexNumbers class
 * Solution by technoname.com
 
 public static void main(String[] args) {
		Scanner s = new Scanner(System.in);

		int real1 = s.nextInt();
		int imaginary1 = s.nextInt();

		int real2 = s.nextInt();
		int imaginary2 = s.nextInt();

		ComplexNumbers c1 = new ComplexNumbers(real1, imaginary1);
		ComplexNumbers c2 = new ComplexNumbers(real2, imaginary2);

		int choice = s.nextInt();
		 
		if(choice == 1) {
			// Add
			c1.plus(c2);
			c1.print();
		}
		else if(choice == 2) {
			// Multiply
			c1.multiply(c2);
			c1.print();
		}
		else {
			return;
		}
	}

public class ComplexNumbers {
	// Complete this class
    int real;
    int imaginary;
    ComplexNumbers(int real ,int imaginary)
    {
        this.real=real;
        this.imaginary=imaginary;
    }
  public void plus(ComplexNumbers c2)
    {
      this.real = this.real+c2.real;
      this.imaginary=this.imaginary+c2.imaginary;
     
    }
	public void print()
    {
        System.out.println(this.real+" + i"+this.imaginary);
    }
    public void multiply(ComplexNumbers c2)
    {
      int r = this.real*c2.real-this.imaginary*c2.imaginary;          
      this.imaginary=this.real*c2.imaginary+this.imaginary*c2.real;
      this.real =r;
     
    }
    
}

Thank you for reading this article so far, if you know some other way to write this program then you can leave a comment here or mail us at technonamecontact@gmail.com, So anyone can a get chance to publish their code on our website. As a result, one can improve knowledge and spread it to others

Please check most asked programming questions of strings with examples by clicking here, after that you might get a deep knowledge about string operations, in short, it will be helpful in clearing the interview.

For understanding the most important concept of data structures in an easy way you can check this article on our website.

Thanks for reading this article so far. If you like this article, then please share it with your friends and colleagues. If you have any questions or feedback, then please drop a note.

You can also follow us on Twitter for daily updates.

Leave a Reply

Your email address will not be published. Required fields are marked *