C++ Articles - Page 667 of 719

C++ Program to Implement Park-Miller Random Number Generation Algorithm

Ravi Ranjan
Updated on 23-Apr-2025 17:02:49

581 Views

The Park-Miller algorithm is a type of linear congruential generator (LCG) that is used to generate pseudo-random numbers. It is also called the minimal standard random number generator. The general formula of a random number generator (RNG) of this type is: X_{k+1} = g X(k) mod n where the modulus n is a prime number or a power of a prime number, the multiplier g is an element of high multiplicative order modulo n, and the seed X0 is co-prime to n. In this article, we will be using Park-Miller algorithm to generate 5 pseudo-random numbers using C++. Here is ... Read More

C++ Program to Generate Random Numbers Using Probability Distribution Function

Ravi Ranjan
Updated on 22-Apr-2025 11:38:55

2K+ Views

The probability density function (pdf) is a function that describes the relative likelihood for this random variable to take on a given value. It is also called as density of a continuous random variable. The probability of the random variable fall within a particular range of values is given by the integral of this variable's density over that range, So, it is given by the area under the density function but above the horizontal axis and between the lowest and greatest values of the range. Probability Distribution is based upon this probability density function. In this article, we will generate ... Read More

C++ Program to Generate Random Numbers Using Middle Square Method

Ravi Ranjan
Updated on 22-Apr-2025 11:38:27

1K+ Views

The middle-square method is one of the simplest methods of generating random numbers. This method will either begin repeatedly generating the same number or cycle to a previous number in the sequence and loop indefinitely. For a generator of n-digit random numbers, the period can be no longer than the specified n(number of digits). If the middle n digits are all zeroes, the generator then generates zeroes forever. In this article, we will implement a C++ program to generate ten 4-digits random number using the middle-square method. Steps for Middle-Square Random Number Generation The steps for generating random numbers ... Read More

C Program to sum the digits of a given number in single statement

Ravi Ranjan
Updated on 15-Apr-2025 15:27:34

592 Views

The calculation of the sum of the digits of a number in a single statement means that only one line of code will be doing the addition operation. The addition operation should not extend more than one statement. In this article, we have a number and our task is to calculate the sum of the digits of the number in a single statement in c. Here is a list of approaches: Using for Loop Using Recursive Function Using for Loop This approach uses a for loop to calculate the ... Read More

C++ Program to Implement the linear congruential generator for Pseudo Random Number Generation

Ravi Ranjan
Updated on 08-May-2025 18:49:19

1K+ Views

The Linear Congruential Generator (LCG) is a very simple technique to generate a sequence of numbers that looks like random numbers but is actually determined. It is one of the reasons to call it a pseudo-random number. The Linear Congruential Generator (LCG) technique generates a random number based on the previous number and uses a linear recurrence to generate the sequence of random numbers. In this article, we have set an initial value of Xn and defined the value of the constants. Our task is to generate pseudo-random numbers using the linear congruential generator in C++. Formula of Linear ... Read More

C Program to print numbers from 1 to N without using semicolon

Ravi Ranjan
Updated on 15-Apr-2025 15:27:59

2K+ Views

Printing numbers from 1 to N is an easy task that can be achieved using loops, but to print numbers from one to N without using a semicolon is a tricky question. We will discuss two different methods to solve this question using iteration and recursion approaches. In this article, our task is to print numbers from 1 to 'n' and we don't have to use a semicolon. The approaches are listed below: Using Iteration Using Recursion Using Iteration This approach uses the iterative approach, where we have used the while loop to print the numbers from 1 to N. The num

C++ Program to Check Whether a Directed Graph Contains a Eulerian Cycle

Ravi Ranjan
Updated on 28-May-2025 12:12:00

657 Views

The Euler path is a path by which we visit every edge exactly once. We can use the same vertices for multiple times. The Euler Circuit is a special type of Euler path. When the starting vertex of the Euler path is also connected with the ending vertex of that path, then it is called the Euler Cycle. In this article, our task is to check if the Eulerian cycle exists in the given directed graph or not. In the above figure, there is a directed graph and its respective adjacency matrix. The Eulerian path respective to the ... Read More

C++ Program to Check Whether a Graph is Strongly Connected or Not

Ravi Ranjan
Updated on 29-May-2025 19:19:15

435 Views

To check if a graph is strongly connected or not, we need to check if for any pair of vertices u and v in the directed graph, there exists a directed path from u to v and a directed path from v to u. In this article, we have a directed graph with five vertices and its respective adjacency matrix. Our task is to check if the given graph is strongly connected or not. Example of Strongly Connected Graph The graph displayed in the figure below is an example of a strongly connected graph. In the above graph, ... Read More

C++ Program to Check the Connectivity of Directed Graph Using DFS

Ravi Ranjan
Updated on 30-May-2025 18:59:12

797 Views

To check if a directed graph is connected or not, we need to check if there exists a path between every pair of vertices. A directed graph (or digraph) is a graph where each edge has a direction, edges are in ordered pairs, and edges traverse from the source vertex (the tail) to the destination vertex (the head). In this article, we have a directed graph with five vertices and its respective adjacency matrix. Our task is to use Depth-first Search(DFS) to check the connectivity of the given graph. Example of Connected Graph In the figure given below, we have ... Read More

C++ Program to Implement Interpolation Search Algorithm

Ravi Ranjan
Updated on 21-Apr-2025 14:40:31

1K+ Views

The interpolation search is an improved version of the binary search. The list is divided using the mid element in the binary search whereas the interpolation search algorithm tries to locate the exact position using the interpolation formula. For this algorithm to work properly, the data collection should be in a sorted form and equally distributed. In this article, we have a sorted array. Our task is to implement the interpolation search algorithm algorithm on this sorted array in C++. The formula used in this algorithm to find the position is given below: Interpolation Position Search Formula pos = ... Read More

Advertisements