Complex numbers java program: In the previous article, we have discussed about Java Program to Find Product of Even and Odd Digits of a Number
In this article we are going to see how to add two complex numbers using Java programming language.
Java Program to Add Two Complex Numbers
Java complex class: Complex numbers consist of two parts, one real part and another imaginary part.
a + bi
- Here a is the real part and b is the imaginary part, i(iota) represents root of -1
- Unfortunately, java doesn’t support complex numbers by default.
- So here we have to use a data structure to hold the two parts of the complex number.
- We will use non static class variables to store the two parts of the complex number. We can use constructors to initialize them. Each object will be a new complex number.
Let’s see different ways to add two complex numbers.
Method 1: Java Program to Add Two Complex Numbers By Taking Static Input Value
Approach:
- Create a new class ComplexNumber and declare two class level variables a and b.
- Create a constructor to initialize them.
- Inside the main method of driver class, create two objects n1 and n2 representing two complex numbers.
- Call the user-defined method to do the addition.
- Define a user-defined method to find the addition.
- It takes two arguments of type ComplexNumber (whatever your class name is).
- Inside the user-defined method, create a new complex number object.
- Update its real value as the sum of real values of the arguments and update imaginary part as the sum imaginary parts of the arguments.
- Print the result inside the main method.
Program:
// class to store complex numbers
class ComplexNum
{
double a;
double b;
// constructor to initialize complex number
ComplexNum(double a, double b)
{
this.a = a;
this.b = b;
}
// method to add two complex numbers
public ComplexNum add(ComplexNum c2)
{
ComplexNum sum = new ComplexNum(this.a + c2.a, this.b + c2.b);
return sum;
}
}
public class AddComplexNums
{
public static void main(String[] args)
{
double d = sc.nextDouble();
// initialize the complex numbers
ComplexNum c1 = new ComplexNum(2, 3);
ComplexNum c2 = new ComplexNum(3, 4);
// call the method to add the two complex numbers
ComplexNum c3 = c1.add(c2);
// print the result
System.out.println("The sum of " + c1.a + " + " + c1.b + "i " + "and "
+ c2.a + " + " + c2.b + "i " + "is " + c3.a + " + " + c3.b + "i");
}
}
Output:
The sum of 2.0 + 3.0i and 3.0 + 4.0i is 5.0 + 7.0i
Method-2: Java Program to Add Two Complex Numbers By Using User Input Value
Approach:
- Inside the driver class create Scanner class object.
- Take user input for two complex numbers and initialize the numbers.
- Use the same procedure as method 1.
Program:
import java.util.Scanner;
// driver class
public class Main
{
public static void main(String[] args)
{
// create scanner class object
Scanner sc = new Scanner(System.in);
// prompt user to enter first complex number
System.out.print("Enter the first complex number: ");
double a = sc.nextDouble();
double b = sc.nextDouble();
// prompt user to enter second complex number
System.out.print("Enter the second complex number: ");
double c = sc.nextDouble();
double d = sc.nextDouble();
// initialize the first complex number
ComplexNum c1 = new ComplexNum(a, b);
// initialize the second complex number
ComplexNum c2 = new ComplexNum(c, d);
// call the method to add the two complex numbers
ComplexNum c3 = c1.add(c2);
// print the sum of the two complex numbers
System.out.println("The sum of " + c1.a + " + " + c1.b + "i " + "and "
+ c2.a + " + " + c2.b + "i " + "is " + c3.a + " + " + c3.b + "i");
}
}
// class to store complex numbers
class ComplexNum
{
double a;
double b;
// constructor to initialize complex number
ComplexNum(double a, double b)
{
this.a = a;
this.b = b;
}
// method to add two complex numbers
public ComplexNum add(ComplexNum c2)
{
ComplexNum sum = new ComplexNum(this.a + c2.a, this.b + c2.b);
return sum;
}
}
Output: Enter the first complex number: 2 3 Enter the second complex number: 3 4 The sum of 2.0 + 3.0i and 3.0 + 4.0i is 5.0 + 7.0i
Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.
Related Java Programs: