In this tutorial, you will learn about a java program to add two complex numbers with example. Complex numbers are made up of two parts: the real part and the imaginary part. We will write a Java program to add two complex numbers in this tutorial.
Example : Add two complex numbers in Java
- ComplexNumber is a class in this program. We have two instance variables in this class, Real and Imaginary, which hold the real and imaginary parts of complex numbers.
- We defined a method sum() to add the two numbers by combining their real and imaginary parts.
- This class constructor is used to initialize the complex numbers. When we create an instance of this class, for example, ComplexNumber temp = new ComplexNumber(0, 0);, we actually create a complex number 0 + 0i.
public class ComplexNumber{ //for real and imaginary parts of complex numbers double real, img; //constructor to initialize the complex number ComplexNumber(double r, double i){ this.real = r; this.img = i; } public static ComplexNumber sum(ComplexNumber c1, ComplexNumber c2) { //creating a temporary complex number to hold the sum of two numbers ComplexNumber temp = new ComplexNumber(0, 0); temp.real = c1.real + c2.real; temp.img = c1.img + c2.img; //returning the output complex number return temp; } public static void main(String args[]) { ComplexNumber c1 = new ComplexNumber(5.5, 4); ComplexNumber c2 = new ComplexNumber(1.2, 3.5); ComplexNumber temp = sum(c1, c2); System.out.printf("Sum is: "+ temp.real+" + "+ temp.img +"i"); } }
Output:
Sum is: 6.7 + 7.5i
Annotations in Java are used to provide metadata for your Java code. Because they are metadata, Java annotations do not directly affect the execution of your code, though some types of annotations can. Java annotations were introduced in Java 5 and are still in use today. Annotations in Java are used to provide metadata for your Java code. Because they are metadata, Java annotations do not directly affect the execution of your code, though some types of annotations can. Java annotations were introduced in Java 5 and are still in use today. Annotations in Java are used to provide metadata for your Java code. Because they are metadata, Java annotations do not directly affect the execution of your code, though some types of annotations can. Java annotations were introduced in Java 5 and are still in use today. Annotations in Java are used to provide metadata for your Java code. Because they are metadata, Java annotations do not directly affect the execution of your code, though some types of annotations can. Java annotations were introduced in Java 5 and are still in use today. Annotations in Java are used to provide metadata for your Java code. Because they are metadata, Java annotations do not directly affect the execution of your code, though some types of annotations can. Java annotations were introduced in Java 5 and are still in use today. Annotations in Java are used to provide metadata for your Java code. Because they are metadata, Java annotations do not directly affect the execution of your code, though some types of annotations can. Java annotations were introduced in Java 5 and are still in use today. Annotations in Java are used to provide metadata for your Java code. Because they are metadata, Java annotations do not directly affect the execution of your code, though some types of annotations can. Java annotations were introduced in Java 5 and are still in use today. Annotations in Java are used to provide metadata for your Java code. Because they are metadata, Java annotations do not directly affect the execution of your code, though some types of annotations can. Java annotations were introduced in Java 5 and are still in use today. Annotations in Java are used to provide metadata for your Java code. Because they are metadata, Java annotations do not directly affect the execution of your code, though some types of annotations can. Java annotations were introduced in Java 5 and are still in use today. Annotations in Java are used to provide metadata for your Java code. Because they are metadata, Java annotations do not directly affect the execution of your code, though some types of annotations can. Java annotations were introduced in Java 5 and are still in use today.
In this tutorial, you will learn about a java program to add two complex numbers with example. In this tutorial, you will learn about a java program to add two complex numbers with example.In this tutorial, you will learn about a java program to add two complex numbers with example. In this tutorial, you will learn about a java program to add two complex numbers with example.