Categories
Java

Addition of two complex numbers passing object as arguments

java program to add two complex numbers by passing objects as arguments in a user defined method.

import java.io.*;
class Complex
{
int rp,ip;
void get()throws Exception
{
System.out.println("Enter real part and imaginary part");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
rp=Integer.parseInt(br.readLine());
ip=Integer.parseInt(br.readLine());
}
void pro(Complex a,Complex b)
{	
	rp=a.rp+b.rp;
	ip=a.ip+b.ip;
}
void show()
{
	if(ip>0)
		System.out.println(rp+"+"+ip+"i");
	else
		System.out.println(rp+" "+ip+"i");
}
}
class CMain
{
	public static void main(String arg[])throws Exception
	{
		Complex a=new Complex();
		Complex b=new Complex();
		Complex c=new Complex();
		a.get();
		b.get();
		c.pro(a,b);
		c.show();	
	}	
}

Leave a Reply

Your email address will not be published. Required fields are marked *