Skip to main content

Featured

C Program to Check Prime Number Using Efficient Logic

  Introduction A prime number is a number that has exactly two distinct positive divisors: 1 and itself. In this program, we check whether a given number is prime or not using a simple and efficient logic. This type of program is commonly used in mathematics, competitive programming, and basic algorithm learning for beginners in C programming. Problem Statement The task is to write a C program that determines whether a given integer is a prime number or not. The program takes a single integer input from the user and analyzes its divisibility. If the number has no divisors other than 1 and itself, it should be identified as a prime number; otherwise, it is not prime. This problem is important in number theory and has practical relevance in areas such as cryptography, data validation, and algorithm design.  Algorithm / Logic Explanation To check whether a number is prime, we need to verify that it is not divisible by any number other than 1 and itself. The algorithm follows a si...

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

๐ŸŒ™