Categories
Java

Addition of two Complex Numbers using object as a return value

java program to add two complex numbers using object as a return value.

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());

}
Complex pro(Complex b)
{
	Complex c=new Complex();
	c.rp=rp+b.rp;
	c.ip=ip+b.ip;
	return(c);
	
}
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=a.pro(b);
		c.show();
		}
}

Leave a Reply

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