Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

Addition of two complex numbers in Java

  1. class Complex
  2. {
  3. int Real,Imag;
  4. Complex()
  5. {}
  6. Complex(int Real1,int Imag1)
  7. {
  8. Real=Real1;
  9. Imag=Imag1;
  10. }
  11. Complex AddComplex(Complex C1,Complex C2)
  12. {
  13. Complex CSum=new Complex();
  14. CSum.Real=C1.Real+C2.Real;
  15. CSum.Imag=C1.Imag+C2.Imag;
  16. return CSum;
  17. }
  18. }
  19.  
  20. class Complexmain
  21. {
  22. public static void main(String[] a)
  23. {
  24. Complex C1=new Complex(4,8);
  25. Complex C2=new Complex(5,7);
  26. Complex C3=new Complex();
  27. C3=C3.AddComplex(C1,C2);
  28. System.out.println("SUM:" + C3.Real +"+i" + C3.Imag);
  29. }
  30. }
  31.  


<table>
Output:
SUM:9+i15
</code>