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 to Subtract Two Complex Numbers using Operator Overloading

C++ Program to Subtract Two Complex Numbers using Operator Overloading

✅ C++ Program to Subtract Two Complex Numbers using Operator Overloading

#include<iostream>
using namespace std;
class complex
{
    public:
    int real,imag;
    void input()
    {
        cout<<"Enter real part:\n";
        cin>>real;
        cout<<"Enter imag part:\n";
        cin>>imag;
    }
    complex()
    {
        real=0;
        imag=0;
    }
    complex(int r,int i )
    {
        real=r;
        imag=i;
    }
    
    complex operator-(complex&obj)
    {
        complex result;
        result.real=real-obj.real;
        result.imag=imag-obj.imag;
        return result;
    }
    void display()
    {
        cout<<real<<"-"<<imag<<"i"<<"\n";
    }
};
int main( )
{
    complex c1,c2,c3;
    cout<<"Enter first part:\n";
    c1.input();
    cout<<"Enter second part:\n";
    c2.input();
    cout<<"Result after subtraction:\n";
    c3=c1-c2;
    c3.display();
}
  

๐Ÿ“˜ Explanation:

This program demonstrates operator overloading in C++. The - operator is overloaded to subtract two complex numbers.

  • Constructors: Default and parameterized constructors are used.
  • operator-() subtracts the real and imaginary parts of two complex numbers.
  • display() prints the result in the standard complex number format.

๐Ÿงพ Sample Output:

Enter first part:
Enter real part:
5
Enter imag part:
3
Enter second part:
Enter real part:
2
Enter imag part:
1
Result after subtraction:
3-2i
  

๐Ÿ”‘ Keywords:

C++ program complex number subtraction, operator overloading in C++, OOP concepts in C++, C++ interview program, complex class in C++

๐Ÿ“Œ Hashtags:

#CPlusPlus #OperatorOverloading #ComplexNumbers #OOP #CodingInterview

๐Ÿ” Search Description:

C++ program to subtract two complex numbers using operator overloading. Demonstrates constructors, operator overloading, and object-oriented programming concepts.

Comments

Popular Posts

๐ŸŒ™