Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

How to add two or more complex numbers in Java ?

Program to demonstrate addition of two or more than two complex numbers in Java

package com.hubberspot.example;

import java.util.Scanner;

public class ComplexNumbers {

   private int real;
   private int imaginary;
   private static String iota = "i";

   public static void main(String[] args) {

 Scanner scanner = new Scanner(System.in);

 System.out.println("Enter no. of complex no's > 2 : ");

 int terms = scanner.nextInt();

 while(true){
           if (terms == 0 || terms == 1 || terms < 0){
       System.out.println("Enter no. of complex no's > 1 : ");
       terms = scanner.nextInt();
    }else { 
       break;
    }   
 }
  
 int[][] complexArray = new int [terms][2];
  
 for(int i = 0; i < terms; i++){
    System.out.println("Enter real number : ");
    complexArray[i][0] = scanner.nextInt();
    System.out.println("Enter imaginary number : ");
    complexArray[i][1] = scanner.nextInt();
 }
  
 int realresult = 0 ;
 int imaginaryresult = 0 ;
  
 for(int i = 0; i < terms - 1; i++){
   
    realresult = complexArray[i][0] + complexArray[i+1][0];
   
 }
  
        for(int i = 0; i < terms - 1; i++){
   
     imaginaryresult = complexArray[i][1] + complexArray[i+1][1];
   
 }
  
 System.out.printf("The resultant complex Number is : %d + %d%s" , 
     realresult,imaginaryresult,iota);
 }

}


Output of the program : 


 
 
© 2021 Learn Java by Examples Template by Hubberspot