Thursday, 24 November 2016

Java Program to adding two Complex Numbers

Java Program to add two Complex Numbers


import java.util.*;
class complex
 {
   public int real, imag;
   public void get()
    {
      Scanner s=new Scanner(System.in);
      System.out.println("Enter the Real part ");
      real= s.nextInt();
      System.out.println("Enter the imaginary part ");
      imag=s.nextInt();
    }

  public void disp()
   {
     System.out.println(""+real +"+"+imag +"i");
   }
  public complex add(complex c1)
   {
    complex temp =new complex();
    temp.real=real+c1.real;
    temp.imag=imag+c1.imag;
    return temp;
   }
}
class compladd
 {
  public static void main(String args[])
  {
    complex a=new complex();
    complex b=new complex();
    complex c=new complex();
    System.out.println("Enter the First complex number :");
    a.get();
    System.out.println("Enter the Second complex number :");
    b.get();
    c=a.add(b);
    System.out.println("First complex number :");
    a.disp();
    System.out.println("Second complex number :");
    b.disp();
    System.out.println("Added  complex number :");
    c.disp();
   }

 }

No comments:

Post a Comment