{"id":1378,"date":"2022-03-03T19:09:18","date_gmt":"2022-03-03T19:09:18","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1378"},"modified":"2022-03-03T19:09:18","modified_gmt":"2022-03-03T19:09:18","slug":"program-to-add-two-complex-numbers","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/03\/03\/program-to-add-two-complex-numbers\/","title":{"rendered":"Program to Add Two Complex Numbers"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Adding two complex numbers in Java<\/h2>\n\n\n\n<p>In this program we have a class&nbsp;<code>ComplexNumber<\/code>. In this class we have two instance variables&nbsp;<code>real<\/code>&nbsp;and&nbsp;<code>img<\/code>&nbsp;to hold the real and imaginary parts of complex numbers.<\/p>\n\n\n\n<p>We have declared a method sum() to\u00a0add the two numbers\u00a0by adding their real and imaginary parts together.<\/p>\n\n\n\n<p>The constructor of this class is used for initializing the complex numbers. For e.g. when we create an instance of this class like this&nbsp;<code>ComplexNumber temp = new ComplexNumber(0, 0);<\/code>, it actually creates a complex number&nbsp;<code>0 + 0i<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class ComplexNumber{\n   \/\/for real and imaginary parts of complex numbers\n   double real, img;\n\t\n   \/\/constructor to initialize the complex number\n   ComplexNumber(double r, double i){\n\tthis.real = r;\n\tthis.img = i;\n   }\n\t\n   public static ComplexNumber sum(ComplexNumber c1, ComplexNumber c2)\n   {\n\t\/\/creating a temporary complex number to hold the sum of two numbers\n        ComplexNumber temp = new ComplexNumber(0, 0);\n\n        temp.real = c1.real + c2.real;\n        temp.img = c1.img + c2.img;\n        \n        \/\/returning the output complex number\n        return temp;\n    }\n    public static void main(String args&#91;]) {\n\tComplexNumber c1 = new ComplexNumber(5.5, 4);\n\tComplexNumber c2 = new ComplexNumber(1.2, 3.5);\n        ComplexNumber temp = sum(c1, c2);\n        System.out.printf(\"Sum is: \"+ temp.real+\" + \"+ temp.img +\"i\");\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Sum is: 6.7 + 7.5i<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Adding two complex numbers in Java In this program we have a class&nbsp;ComplexNumber. In this class we have two instance variables&nbsp;real&nbsp;and&nbsp;img&nbsp;to hold the real and imaginary parts of complex numbers. We have declared a method sum() to\u00a0add the two numbers\u00a0by adding their real and imaginary parts together. The constructor of this class is used for [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[260],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1378"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1378"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1378\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1378"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1378"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1378"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}