Pin It

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 : 


 
Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot