C++ Articles

Page 291 of 597

Complex numbers in C++

George John
George John
Updated on 11-Mar-2026 10K+ Views

In this section we will see how to create and use complex numbers in C++. We can create complex number class in C++, that can hold the real and imaginary part of the complex number as member elements. There will be some member functions that are used to handle this class.In this example we are creating one complex type class, a function to display the complex number into correct format. Two additional methods to add and subtract two complex numbers etc.Example#include using namespace std; class complex {    int real, img;    public:       complex() {     ...

Read More

C++ Program to use above below primitive to test whether two lines intersect

Anvi Jain
Anvi Jain
Updated on 11-Mar-2026 260 Views

Here is a C++ program to use above below primitive to test whether two lines intersect. It can be used to test whether a line intersects a line segment. It does if and only if one endpoint of the segment is to the left of the line and the other is to the right.AlgorithmBegin    For generating equation of the first line, generate random numbers for coefficient of x and y by using rand at every time of compilation.    For generating equation of the second line, generate random numbers for coefficient of x and y by using rand at ...

Read More

C++ Program to Implement the RSA Algorithm

Anvi Jain
Anvi Jain
Updated on 11-Mar-2026 19K+ Views

RSA is an asymmetric cryptography algorithm which works on two keys-public key and private key.AlgorithmsBegin    1. Choose two prime numbers p and q.    2. Compute n = p*q.    3. Calculate phi = (p-1) * (q-1).    4. Choose an integer e such that 1 < e < phi(n) and gcd(e, phi(n)) = 1; i.e., e and phi(n) are coprime.    5. Calculate d as d ≡ e−1 (mod phi(n)); here, d is the modular multiplicative inverse of e modulo phi(n).    6. For encryption, c = me mod n, where m = original message.    7. For ...

Read More

How to stop C++ console application from exiting immediately?

Anvi Jain
Anvi Jain
Updated on 11-Mar-2026 1K+ Views

Sometimes we have noticed that the console is being closed immediately after displaying the result. So we cannot see the result properly. Here we will see how we can stop the console from closing.The idea is very simple. We can use the getchar() function at the end. This will wait for one character. If one character is pressed, the console will exit.Example#include using namespace std; int main() {    cout

Read More

C++ Program to Implement Johnson's Algorithm

Nishtha Thakur
Nishtha Thakur
Updated on 11-Mar-2026 1K+ Views

Here we will see the Johnson’s Algorithm to find shortest path between two vertices. The graph is given here. The shortest path between the edges is like below. This program will take the number of vertices, number of edges, and the edges with their costs.Input − Vertices: 3Edges: 5Edge with costs −1 2 82 1 121 3 223 1 62 3 4Output − The distance matrix of the graph.081210046140AlgorithmjohnsonAlgorithm(cost)Input − The cost matrix of given Graph.Output − Matrix to for shortest path between any vertex to any vertex.Begin    Create another matrix ‘A’ same as cost matrix, if there is no ...

Read More

isnormal() in C++

Smita Kapse
Smita Kapse
Updated on 11-Mar-2026 155 Views

In this section we will see the isnormal() function in C++. This function is present in the cmath library. This function is used to check whether a number is normal or not. The numbers that are considered as non-normal are zeros, infinity or NAN.This function takes float, double or long double values as argument. Returns 1 if the number is normal, otherwise returns 0.Example#include #include using namespace std; int main() {    cout

Read More

When should we write our own assignment operator in C++?

Nishtha Thakur
Nishtha Thakur
Updated on 11-Mar-2026 507 Views

Here we will see when we need to create own assignment operator in C++. If a class do not have any pointers, then we do not need to create assignment operator and copy constructor. C++ compiler creates copy constructor and assignment operator for each class. If the operators are not sufficient, then we have to create our own assignment operator.Example#include using namespace std; class MyClass { //no user defined assignment operator or copy constructor is present    int *ptr;    public:       MyClass (int x = 0) {          ptr = new int(x);       }    void setValue (int x) {       *ptr = x;    }    void print() {       cout

Read More

Comparison of Exception Handling in C++ and Java

Smita Kapse
Smita Kapse
Updated on 11-Mar-2026 353 Views

The exception handling feature is present almost any object oriented languages nowadays. In C++ and Java also we can get this kind of feature. There are some similarities between exception handling in C++ and exception handling in Java, like in both languages we have to use the try-catch block. Though there are some difficulties also. These are like below −In C++, we can throw any type of data as exception. Any type of data means primitive datatypes and pointers also. In Java we can only throw the throwable objects. Subclasses of any throwable class will also be throwable.Example#include using ...

Read More

Shuffle vs random_shuffle in C++

Anvi Jain
Anvi Jain
Updated on 11-Mar-2026 622 Views

Here we will see the Shuffle and random_shuffle in C++. Let us see the random_shuffle first. It is used to randomly rearrange the elements in range [left, right). This function randomly swaps the positions of each element with the position of some randomly chosen positions.We can provide some random generator function to tell which element will be taken in every case. If we do not provide some, it will use its own random generator function.Example#include using namespace std; int myRandomGenerator(int j) {    return rand() % j; } main() {    srand(unsigned(time(0)));    vector arr;    for (int j ...

Read More

Bind function and placeholders in C++

Smita Kapse
Smita Kapse
Updated on 11-Mar-2026 2K+ Views

Here we will see the Bind function and the placeholders in C++. Sometimes we need to manipulate the operation of some functions as we need. We can use some default parameters to get some essence of manipulating.In C++11, one new feature is introduced, called the bind function. This helps us to do such manipulating in some easier fashion. To use these features, we have to use header file.Bind functions with the help of placeholders helps to determine the positions, and number of arguments to modify the function according to desired outputs.Placeholders are namespaces which detect the position of a ...

Read More
Showing 2901–2910 of 5,962 articles
« Prev 1 289 290 291 292 293 597 Next »
Advertisements