Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C++ Data Types
  • C++ Input/Output
  • C++ Arrays
  • C++ Pointers
  • C++ OOPs
  • C++ STL
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ MCQ
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management
Open In App
Next Article:
C++ program to implement Full Adder
Next article icon

C++ Program To Add Two Complex Numbers

Last Updated : 23 Jun, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Given two complex numbers of the form and the task is to add these two complex numbers.

a1 + ib1  and  a2 + ib2

Here the values of real and imaginary numbers are passed while calling the parameterized constructor and, with the help of a default(empty) constructor, the function addComp is called to get the addition of complex numbers.

Examples:

Input: a1 = 4, b1 = 8         a2 = 5, b2 = 7 Output: Sum = 9 + i15   Explanation: (4 + i8) + (5 + i7)            = (4 + 5) + i(8 + 7)             = 9 + i15

Below is the C++ program to add two complex numbers:

C++
// C++ Program to Add  // Two Complex Numbers  // Importing all libraries #include<bits/stdc++.h> using namespace std;   // User Defined Complex class class Complex  {      // Declaring variables     public: int real, imaginary;       // Constructor to accept     // real and imaginary part     Complex(int tempReal = 0,              int tempImaginary = 0)     {         real = tempReal;         imaginary = tempImaginary;     }       // Defining addComp() method     // for adding two complex number     Complex addComp(Complex C1, Complex C2)     {         // Creating temporary variable         Complex temp;           // Adding real part of          // complex numbers         temp.real = C1.real + C2.real;           // Adding Imaginary part of          // complex numbers         temp.imaginary = (C1.imaginary + C2.imaginary);           // Returning the sum         return temp;     } };   // Driver code int main() {     // First Complex number     Complex C1(3, 2);      // printing first complex number     cout << "Complex number 1 : " <<               C1.real << " + i" <<               C1.imaginary << endl;      // Second Complex number     Complex C2(9, 5);      // Printing second complex number     cout << "Complex number 2 : " <<               C2.real << " + i" <<               C2.imaginary << endl;      // For Storing the sum     Complex C3;      // Calling addComp() method     C3 = C3.addComp(C1, C2);      // Printing the sum     cout << "Sum of complex number : " <<               C3.real << " + i" <<               C3.imaginary; } 

Output
Complex number 1 : 3 + i2 Complex number 2 : 9 + i5 Sum of complex number : 12 + i7

Explanation of the above method

  1.  A class Complex is created for complex numbers with two data members real and imaginary, a parameterized constructor, and a function to add complex numbers.
  2.  Parameterized constructor is used to initialize the data members real and imaginary.
  3.  A function addComp() with Class return type is created to add two complex numbers.

The complexity of the above method

Time Complexity: O(1)

Auxiliary Space: O(1)


Next Article
C++ program to implement Full Adder
author
kartik
Improve
Article Tags :
  • C++ Programs
  • C++
  • C Basic Programs
Practice Tags :
  • CPP

Similar Reads

  • C++ Program to Swap Two Numbers
    Swapping numbers is the process of interchanging their values. In this article, we will learn algorithms and code to swap two numbers in the C++ programming language. 1. Swap Numbers Using a Temporary VariableWe can swap the values of the given two numbers by using another variable to temporarily st
    4 min read
  • C++ Program To Multiply Two Floating-Point Numbers
    Here, we will see how to multiply two floating-point numbers using the C++ program. Floating point numbers are numbers that include both integer part and fractional parts represented in the form of decimal and exponential values. float data type is used to store floating point values. For example In
    2 min read
  • C++ program for Complex Number Calculator
    Pre-Requisites: Complex Numbers, Mathematics, Object-oriented Programming This is a Complex Number Calculator which performs a lot of operations which are mentioned below, to simplify our day-to-day problems in mathematics. The implementation of this calculator is done using C++ Programming Language
    15+ min read
  • C++ program to implement Full Adder
    Prerequisite : Full AdderWe are given three inputs of Full Adder A, B,C-IN. The task is to implement the Full Adder circuit and Print output i.e. sum and C-Out of three inputs. Introduction : A Full Adder is a combinational circuit that performs an addition operation on three 1-bit binary numbers. T
    2 min read
  • Add Two Numbers in C++
    Given two integers, the task is to add these integer number and print their sum in C++. Examples Input: a = 11, b = 9Output: 20Explanation: Sum of 11 + 9 = 20 Input: a = 1, b = 8Output: 9Explanation: Sum of 1 + 8 = 9 Add Two Numbers Using Addition OperatorIn C++, the simplest method for adding the t
    3 min read
  • How to add two Hexadecimal numbers?
    Given two numeric Hexadecimal numbers str1 and str2, the task is to add the two hexadecimal numbers. Hexadecimal Number system, often shortened to “hex”, is a number system made up from 16 symbols. it uses 10 symbols from decimal number system which are represented by 0-9 and six extra symbols A - F
    15+ min read
  • C++ Program To Add Two Numbers Represented By Linked Lists- Set 1
    Given two numbers represented by two lists, write a function that returns the sum list. The sum list is a list representation of the addition of two input numbers. Example: Input: List1: 5->6->3 // represents number 563 List2: 8->4->2 // represents number 842 Output: Resultant list: 1-
    12 min read
  • C++ Program For Addition of Two Matrices
    Given two N x M matrices. Find a N x M matrix as the sum of given matrices each value at the sum of values of corresponding elements of the given two matrices. In this article, we will learn about the addition of two matrices. ApproachBelow is the idea to solve the problem. Iterate over every cell o
    2 min read
  • C++ Program To Find Compound Interest
    What is 'Compound interest'? Compound interest is the addition of interest to the principal sum of a loan or deposit, or in other words, interest on interest. It is the result of reinvesting interest, rather than paying it out, so that interest in the next period is then earned on the principal sum
    2 min read
  • C++ Program To Find Armstrong Numbers Between Two Integers
    A positive integer with digits a, b, c, d... is called an Armstrong number of order n if following condition is satisfied.  abcd... = an + bn + cn + dn +... 153 = 1*1*1 + 5*5*5 + 3*3*3 = 1 + 125 + 27 = 153 Therefore, 153 is an Armstrong number. Examples: Input: 100 400 Output: 153 370 371 Explanatio
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences