-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComplexNumbers.java
More file actions
68 lines (60 loc) · 2.08 KB
/
Copy pathComplexNumbers.java
File metadata and controls
68 lines (60 loc) · 2.08 KB
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
66
67
68
/*Qus 5:- A complex number is a number that can be expressed in the form a+bi,
where a
and b are real numbers, and i is a solution of equation x2=-1.
Write a program to Add and subtract two complex numbers by creating a class
Complex number in which :
• The complex numbers will be initialized with the help of the constructor.
• The addition and subtraction will be performed with the help of function
calling.
Example :
Input : a1= 4 b1=8 a2=5 b2=7
Output : Sum = 9+ i15 Difference = -1+i
*/
import java.util.Scanner;
class ComplexNumber {
private int a;
private int b;
// Constructor to initialize the complex number
public ComplexNumber(int a, int b) {
this.a = a;
this.b = b;
}
// Method to add two complex numbers
public ComplexNumber add(ComplexNumber other) {
return new ComplexNumber(this.a + other.a, this.b + other.b);
}
// Method to subtract two complex numbers
public ComplexNumber subtract(ComplexNumber other) {
return new ComplexNumber(this.a - other.a, this.b - other.b);
}
@Override
public String toString() {
if (b >= 0) {
return a + " + i" + b;
} else {
return a + " - i" + (-b);
}
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
// Example input
System.out.println("eneter the a1 value");
int a1 = sc.nextInt();
System.out.println("eneter the a2 value");
int a2 =sc.nextInt();
System.out.println("eneter the b1 value");
int b1=sc.nextInt();
System.out.println("eneter the b2 value");
int b2=sc.nextInt();
// Create two complex number objects
ComplexNumber num1 = new ComplexNumber(a1, b1);
ComplexNumber num2 = new ComplexNumber(a2, b2);
// Perform addition and subtraction
ComplexNumber sum = num1.add(num2);
ComplexNumber difference = num1.subtract(num2);
// Print the results
System.out.println("Sum = " + sum);
System.out.println("Difference = " + difference);
}
}