Sunday, October 21, 2012

Java program to perform complex number addition


import java.io.*;
class complex
{
   int r,i;
   complex()
{
   r =0;
   i=0;
}
  void sum(complex c,complex d)
{
complex a=new complex();
a.r=c.r+d.r;
a.i=c.i+d.i;
     System.out.println("The sum is "+a.r+"+"+a.i+"i");
  }
  void read()throws IOException
  {
 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  System.out.println("\nEnter the real part");
   r=Integer.parseInt(br.readLine());
  System.out.println("\nEnter the imaginary part");
    i=Integer.parseInt(br.readLine());
  System.out.println("\nYou have entered : "+r+"+"+i+"i");
  }
  public static void main(String s[])throws IOException
  { 
     complex c=new complex();
     complex d=new complex();
     System.out.println("First number");
     c.read();
     System.out.println("\nSecond number");
     d.read();
     d.sum(c,d);
  }
}
Output

No comments:

Post a Comment