-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalcs_On_Complex_Numbers.java
70 lines (48 loc) · 1.53 KB
/
Calcs_On_Complex_Numbers.java
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Java program to add two complex numbers
class ComplexNumber {
// variables to hold real and imaginary part of complex
// number
int real, image;
// Constructor which will be used while creating complex
// number
public ComplexNumber(int r, int i)
{
this.real = r;
this.image = i;
}
// function to print real number
public void showC()
{
System.out.print(this.real + " +i" + this.image);
}
// function for addition
public static ComplexNumber add(ComplexNumber n1,
ComplexNumber n2)
{
// creating blank complex number
// to store result
ComplexNumber res = new ComplexNumber(0, 0);
// adding real parts of both complex numbers
res.real = n1.real + n2.real;
// adding imaginary parts
res.image = n1.image + n2.image;
// returning result
return res;
}
public static void main(String arg[])
{
// creating two complex numbers
ComplexNumber c1 = new ComplexNumber(4, 5);
ComplexNumber c2 = new ComplexNumber(10, 5);
// printing complex numbers
System.out.print("first Complex number: ");
c1.showC();
System.out.print("\nSecond Complex number: ");
c2.showC();
// calling add() to perform addition
ComplexNumber res = add(c1, c2);
// displaying addition
System.out.println("\nAddition is :");
res.showC();
}
}