-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComplex.java
More file actions
38 lines (32 loc) · 1006 Bytes
/
Complex.java
File metadata and controls
38 lines (32 loc) · 1006 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.util.Scanner;
class Complex{
int real,imag;
Complex(int real,int imag){
this.real=real;
this.imag=imag;
}
Complex add(Complex a){
Complex temp=new Complex(0,0);
temp.real=this.real+a.real;
temp.imag=this.imag+a.imag;
return temp;
}
Complex multiply(Complex a){
Complex temp= new Complex(0,0);
temp.real=this.real*a.real-this.imag*a.imag;
temp.imag=this.real*a.imag+this.imag*a.real;
return temp;
}
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
System.out.println("Enter the first number");
Complex first = new Complex(scan.nextInt(),scan.nextInt());
System.out.println("Enter the second number");
Complex second = new Complex(scan.nextInt(),scan.nextInt());
Complex temp;
temp=first.add(second);
System.out.println("The sum is "+temp.real+" + i"+temp.imag);
temp=first.multiply(second);
System.out.println("The product is "+temp.real+" + i"+temp.imag);
}
}