Math, asked by SatvikVats8737, 10 hours ago

7. declare a structure to represent a complex number (a number having a real part and imaginary part). Write c++ functions to add, subtract, multiply and divide two complex numbers

Answers

Answered by harshittomar266
1

2357892+161818171719272827

Answered by Tulsi4890
1

The C++ function is written below:

#include <iostream>

using namespace std;

class complexNumber {

   public:

       double real;

       double imaginary;

       void add(complexNumber a, complexNumber b) {

       

           double real = a.real + b.real;

           double imaginary = a.imaginary + b.imaginary;

           complexNumber c = complexNumber(real, imaginary);

           cout << "a + b = " << c.real << " + (" << c.imaginary << ") * i" << endl;

       }

       void sub(complexNumber a, complexNumber b) {

     

           double real = a.real - b.real;

           double imaginary = a.imaginary - b.imaginary;

           complexNumber c = complexNumber(real, imaginary);

           cout << "a - b = " << c.real << " + (" << c.imaginary << ") * i" << endl;

       }

       void multiply(complexNumber a, complexNumber b) {

       

           double real = a.real * b.real - a.imaginary * b.imaginary;

           double imaginary = a.imaginary * b.real + a.real * b.imaginary;

           complexNumber c = complexNumber(real, imaginary);

           cout << "a * b = " << c.real << " + (" << c.imaginary << ") * i" << endl;

       }

       void divide(complexNumber a, complexNumber b) {

       

           double real = (a.real * b.real + a.imaginary * b.imaginary) / (b.real * b.real + b.imaginary * b.imaginary);

           double imaginary = (a.imaginary * b.real - a.real * b.imaginary) / (b.real * b.real + b.imaginary * b.imaginary);

           complexNumber c = complexNumber(real, imaginary);

           cout << "a : b = " << c.real << " + (" << c.imaginary << ") * i" << endl;

       }

 

   complexNumber(double real, double imaginary) {

       this->real = real;

       this->imaginary = imaginary;

   }

};

int main() {

   /*

    * Creating variables for the real and imaginary parts

    */

   double realA;

   double imaginaryA;

   double realB;

   double imaginaryB;

   /*

    * Getting user input

    */

   cout << "Enter real part of (A), imaginary part of (A), real part of (B) and imaginary part of (B) >> ";

   cin >> realA >> imaginaryA >> realB >> imaginaryB;

   cout << endl;

   /*

    * Creating two complexNumbers using our struct

    */

   complexNumber a = complexNumber(realA, imaginaryA);

   complexNumber b = complexNumber(realB, imaginaryB);

   /*

    * Calling the functions

    */

   a.add(a, b);

   a.sub(a, b);

   a.multiply(a, b);

   a.divide(a, b);

   return 0;

}

Similar questions