38 Program to perform addition of two complex number in java

Code:

//a+bi

import java.util.*;
import java.io.*;

class Complex
{
public static void main( String args[] )
{
int a1,b1,a2,b2,a3,b3;

Scanner scan = new Scanner(new InputStreamReader(System.in) );

System.out.println("\n\n Enter any two number:");
a1 = scan.nextInt();
b1 = scan.nextInt();

System.out.println("\n\n Enter any two number:");
a2 = scan.nextInt();
b2 = scan.nextInt();

a3 = a1 + a2;
b3 = b1 + b2;

System.out.println("\n\n 1st Complex Number: ");
display(a1,a2);

System.out.println("\n\n 2nd Complex Number: ");
display(a2,b2);

System.out.println("\n\n Complex number Addition: ");
display(a3,b3);
}

static void display(int a,int b)
{
    if(b > 0)
       System.out.println(a + " + " + b + "i" );
    else
  System.out.println( a + "  " + b + "i" );
}
}


Ouput:


Previous
Next Post »