Utopper SkillUtopper Skill
  • Programming
    • Programming Examples
  • Interview Questions
    • DevOps Interview Questions
    • Android Interview Questions
  • How to
  • Tools
  • Top 10
  • Book Summaries
Reading: C++ Program To Add Two Complex Numbers
Share
Utopper SkillUtopper Skill
Search
  • Book Summaries
  • Programming Examples
  • C Programming Example
  • Interview Question
  • How to
  • Top 10
Follow US
Utopper Skill > Programming Examples > C++ Programming Example > C++ Program To Add Two Complex Numbers
Programming ExamplesC++ Programming Example

C++ Program To Add Two Complex Numbers

Utopper Skill Team
By Utopper Skill Team Last updated: March 1, 2023
Share
8 Min Read
C++ Program To Add Two Complex Numbers
SHARE
Table of Content
What is a Complex Number?Approach to follow for C++ Program To Add Two Complex NumbersPseudo Code To Add Two Complex Numbers in C++C++ Program To Add Two Complex NumbersOutput

C++ built-in std::complex class makes complex numbers easy to programme. C++ is a powerful, high-level language that lets programmers write efficient, scalable code for many applications. Scientific computing and engineering often add two complex numbers in C++ using the + operator or a function. C++ is ideal for complex number calculations due to the std::complex class. We’ll write a C++ program to add two complex numbers and discuss the key concepts in this article.

C++ Program To Add Two Complex Numbers

What is a Complex Number?

Complex numbers can be written as a + bi, where a and b are real numbers and I is the imaginary unit, which is the square root of -1. Complex numbers have real and imaginary parts.

On a complex plane, the real axis is horizontal and the imaginary axis is vertical. Real and imaginary parts of a complex number determine its complex plane position.

Math, science, engineering, and other fields use complex numbers. They represent alternating current in electrical engineering and waves in physics. Signal processing, control systems, and other applied mathematics fields use them.

Approach to follow for C++ Program To Add Two Complex Numbers

To write a C++ program to add two complex numbers, we can follow the following approach:

  1. Define a class for Complex numbers with private data members for the real and imaginary parts.
  2. Implement a constructor to initialize the real and imaginary parts of the Complex number.
  3. Implement an operator function to add two Complex numbers using the + operator.
  4. Implement a member function to print the Complex number.
  5. In the main() function, create two Complex objects with initial values, add them using the + operator, and print the sum.

You can Also the C++ Program to Print ASCII Value of Character

Pseudo Code To Add Two Complex Numbers in C++

Here’s a pseudo code to add two complex numbers in C++:

// Define a class for Complex numbers
class Complex {
    private:
        float real;  // Real part of the complex number
        float imag;  // Imaginary part of the complex number

    public:
        // Constructor to initialize the real and imaginary parts
        Complex(float r = 0, float i = 0) {
            real = r;
            imag = i;
        }

        // Operator function to add two Complex numbers
        Complex operator +(Complex const &obj) {
            Complex res;  // Create a new Complex object to store the result
            res.real = real + obj.real;  // Add the real parts
            res.imag = imag + obj.imag;  // Add the imaginary parts
            return res;  // Return the result
        }

        // Function to print the Complex number
        void print() {
            cout << real << " + i" << imag << endl;  // Print the real and imaginary parts
        }
};

// In the main function
int main() {
    Complex num1(2.5, 3.5);  // Create a Complex object with initial values
    Complex num2(1.5, 2.5);  // Create another Complex object with initial values

    Complex sum = num1 + num2;  // Add the two Complex numbers using the + operator

    cout << "Sum of two complex numbers is ";
    sum.print();  // Print the sum of the two Complex numbers

    return 0;
}

C++ Program To Add Two Complex Numbers

To add two complex numbers in C++, you can create a Complex class to represent a complex number with real and imaginary parts. The Complex class should have an operator+ function that adds two Complex objects and returns the result.

#include <iostream>

using namespace std;

// Define a class for Complex numbers
class Complex {
    private:
        float real;  // Real part of the complex number
        float imag;  // Imaginary part of the complex number

    public:
        // Constructor to initialize the real and imaginary parts
        Complex(float r = 0, float i = 0) {
            real = r;
            imag = i;
        }

        // Operator function to add two Complex numbers
        Complex operator +(Complex const &obj) {
            Complex res;  // Create a new Complex object to store the result
            res.real = real + obj.real;  // Add the real parts
            res.imag = imag + obj.imag;  // Add the imaginary parts
            return res;  // Return the result
        }

        // Function to print the Complex number
        void print() {
            cout << real << " + i" << imag << endl;  // Print the real and imaginary parts
        }
};

// In the main function
int main() {
    Complex num1(2.5, 3.5);  // Create a Complex object with initial values
    Complex num2(1.5, 2.5);  // Create another Complex object with initial values

    Complex sum = num1 + num2;  // Add the two Complex numbers using the + operator

    cout << "Sum of two complex numbers is ";
    sum.print();  // Print the sum of the two Complex numbers

    return 0;
}

In this program, we define a class Complex with two private members real and imag to represent a complex number. We also define a constructor to initialize the real and imaginary parts of the complex number, and an operator+ function to add two complex numbers.

In the operator+ function, we create a new Complex object to store the result of the addition of the two complex numbers. We add the real parts and imaginary parts of the two complex numbers and store the result in the new Complex object.

In the print function, we simply print the real and imaginary parts of the complex number.

In the main function, we create two Complex objects num1 and num2 with initial values, and add them using the operator+ function. Finally, we print the sum of the two complex numbers.

Output

The output of the above C++ program to add two complex numbers will be:

Sum of two complex numbers is 4 + i6

This is because the program creates two Complex objects with initial values 2.5 + 3.5i and 1.5 + 2.5i, adds them using the operator+ function, and stores the result in the sum variable. The sum of the two complex numbers is 4 + 6i. The print function is then called to print the result, which outputs 4 + i6.

TAGGED:addition in c++C Program Examplescomplex number example
Share This Article
Facebook Twitter Whatsapp Whatsapp Telegram Copy Link Print
Previous Article Terraform Interview Questions and Answers 40 Best Terraform Interview Questions and Answers 2024
Next Article Kubernetes Interview Questions and Answers Top 40 Kubernetes Interview Questions and Answers 2024 Edition
Most Popular
Learn C Programming (Basic to Advanced)
C If Else Statement
Utopper Skill Author By Utopper Skill Author
Book Summary of Why Nobody Told Me This Before By Dr. Julie Smith
Book Summary of Why Nobody Told Me This Before? By Dr. Julie Smith
Utopper Skill Author By Utopper Skill Author
Learn C Programming (Basic to Advanced)
Data Type in C Programming
Utopper Skill Author By Utopper Skill Author
Book Summary of Surrounded By Idiots By Thomas Erikson
Book Summary of Surrounded by Idiots By Thomas Erikson
Utopper Skill Author By Utopper Skill Author
Top 10 Ways to Boost Your Productivity at Work
Top 10 Ways to Boost Your Productivity at Work
Utopper Skill Team By Utopper Skill Team

You Might Also Like

C++ Program To Print Prime Numbers From 1 To N
C Programming ExampleProgramming Examples

C++ Program To Print Prime Numbers From 1 To N

11 Min Read
C Program to Print Your Own Name
Programming ExamplesC Programming Example

C Program to Print Your Own Name with Example

2 Min Read
C Program To Convert Fahrenheit To Celsius
C Programming ExampleProgramming Examples

C Program To Convert Fahrenheit To Celsius

5 Min Read
C Program To Find LCM of Two Numbers
Programming ExamplesC Programming Example

C Program To Find LCM of Two Numbers

6 Min Read

Mail : [email protected]

Privacy Policy | DISCLAIMER | Contact Us

Learn

  • learn HTML
  • learn CSS
  • learn JavaScript

Examples

  • C Examples
  • C++ Examples
  • Java Examples

Study Material

  • Interview Questions
  • How to
  • Hosting
  • SEO
  • Blogging

 

© 2024 : UTOPPER.COM | Owned By : GROWTH EDUCATION SOLUTIONS PRIVATE LIMITED
Welcome Back!

Sign in to your account

Lost your password?