Skip to main content

Featured

C Program to Solve Two Sum Using Brute Force (With Algorithm & Output)

 Introduction The Two Sum problem is a popular coding interview question where we must find two indices of an array whose values add up to a given target. This program demonstrates a simple brute-force solution in C using nested loops and dynamic memory allocation. Problem Statement Given an integer array and a target value, return the indices of the two numbers such that they add up to the target. Each input has exactly one solution, and the same element cannot be used twice. The result should return the indices, not the values. If no solution exists, return NULL.  Algorithm / Logic Explanation Start the program. Traverse the array using a loop from index 0 to numsSize - 1 . Inside this loop, use another loop starting from i + 1 to numsSize - 1 . For every pair (i, j) , check if nums[i] + nums[j] == target . If condition becomes true: Allocate memory for 2 integers using malloc() . Store indices i and j . Set returnSize = 2 . Return the result poi...

C++ Program for Complex Numbers Using Operator Overloading

C++ Program for Complex Numbers Using Operator Overloading

✅ C++ Program for Complex Number Operations Using Operator Overloading

#include <iostream>
#include <cmath>
using namespace std;

class complex
{
public:
    float real, image;

    complex()
    {
        real = 0;
        image = 0;
    }

    void input()
    {
        cout << "Enter real part: ";
        cin >> real;
        cout << "Enter imaginary part: ";
        cin >> image;
    }

    complex operator+(complex &obj)
    {
        complex temp;
        temp.real = real + obj.real;
        temp.image = image + obj.image;
        return temp;
    }

    complex operator-(complex &obj)
    {
        complex temp;
        temp.real = real - obj.real;
        temp.image = image - obj.image;
        return temp;
    }

    complex operator*(complex &obj)
    {
        complex temp;
        temp.real = (real * obj.real) - (image * obj.image);
        temp.image = (real * obj.image) + (image * obj.real);
        return temp;
    }

    complex operator/(complex &obj)
    {
        complex temp;
        float denom = (obj.real * obj.real) + (obj.image * obj.image);

        temp.real = (real * obj.real + image * obj.image) / denom;
        temp.image = (image * obj.real - real * obj.image) / denom;

        return temp;
    }

    complex conjugate()
    {
        complex temp;
        temp.real = real;
        temp.image = -image;
        return temp;
    }

    float modulus()
    {
        return sqrt((real * real) + (image * image));
    }

    void display()
    {
        cout << real << " + " << image << "i" << endl;
    }
};

int main()
{
    complex c1, c2, c3;

    cout << "Enter first complex number:\n";
    c1.input();

    cout << "\nEnter second complex number:\n";
    c2.input();

    c3 = c1 + c2;
    cout << "Addition:\n";
    c3.display();

    c3 = c1 - c2;
    cout << "Subtraction:\n";
    c3.display();

    c3 = c1 * c2;
    cout << "Multiplication:\n";
    c3.display();

    c3 = c1 / c2;
    cout << "Division:\n";
    c3.display();

    c3 = c1.conjugate();
    cout << "Conjugate:\n";
    c3.display();

    cout << "Modulus:\n";
    cout << c1.modulus() << endl;

    return 0;
}
  

๐Ÿ“˜ Explanation:

This program demonstrates operator overloading in C++ for performing arithmetic operations on complex numbers. Each operator is overloaded using mathematical formulas.

  • + → Adds two complex numbers
  • - → Subtracts two complex numbers
  • * → Multiplies two complex numbers
  • / → Divides two complex numbers
  • Conjugate → Changes sign of imaginary part
  • Modulus → Calculates magnitude

๐Ÿ“ Mathematical Formulas Used:

Let z₁ = a + bi
    z₂ = c + di

Addition       :(a+bi)+(c+di)=(a+c)+(b+d)i
Subtraction    :(a+bi)−(c+di)=(a−c)+(b−d)i
Multiplication : (a+bi)(c+di)=(ac−bd)+(ad+bc)i
Division       :a+bi/c+di=(ac+bd)+(bc-ad)i/c^2+d^2
Conjugate      : a − bi
Modulus        : √(a² + b²)
  

๐Ÿงพ Sample Output:

Enter real part: 4
Enter imaginary part: 5
Enter real part: 2
Enter imaginary part: 3

Addition:
6 + 8i
Subtraction:
2 + 2i
Multiplication:
-7 + 22i
Division:
1.77 + -0.15i
Conjugate:
4 + -5i
Modulus:
6.40
  

๐Ÿ”‘ Keywords:

C++ complex number program, operator overloading, complex arithmetic, C++ OOP example, complex class in C++

๐Ÿ“Œ Hashtags:

#CPlusPlus #OperatorOverloading #ComplexNumbers #OOP #CPPBasics #Programming #1printf

๐Ÿ” Search Description:

Learn how to perform complex number operations in C++ using operator overloading. Includes formulas, explanation, sample output, and dark-themed code.

Comments

Popular Posts

๐ŸŒ™