Found 34484 Articles for Programming

C++ Program to Emulate N Dice Roller

Nancy Den
Updated on 30-Jul-2019 22:30:25

893 Views

Here is the code to emulate N dice roller. This can be done by generating random number between 1-6.AlgorithmBegin    Declare n    Read n    For i = 0 to n-1 do       Generate sequence with rand() mod 6 + 1       Print the sequence    Done EndExample Code#include using namespace std; int main(int argc, char **argv) {    cout > n;    cout

C Program to print hollow pyramid and diamond pattern

George John
Updated on 30-Jul-2019 22:30:25

3K+ Views

Here we will see how to generate hollow pyramid and diamond patterns using C. We can generate solid Pyramid patterns very easily. To make it hollow, we have to add some few tricks.Hollow PyramidFor the pyramid at the first line it will print one star, and at the last line it will print n number of stars. For other lines it will print exactly two stars at the start and end of the line, and there will be some blank spaces between these two starts.Example Code#include int main() {    int n, i, j;    printf("Enter number of lines: ... Read More

C program to print digital clock with current time

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

3K+ Views

In this section we will see how to make a digital clock using C. To work with time we can use the time.h header file. This header file has some function signatures that are used to handle date and time related issues.The four important components of time.h is like belowsize_t This size_t is basically the unsigned integral type. This is the result of sizeof().clock_t This is used to store the processor timetime_t This is used to store calendar timestruct tm This is a structure. It helps to hold the entire date and time.Example Code#include #include int main() { ... Read More

C++ Program to Generate Random Numbers Using Multiply with Carry Method

Nancy Den
Updated on 30-Jul-2019 22:30:25

211 Views

The multiply-with-carry method is a variant of the add-with-carry generator introduced by Marsaglia and Zaman (1991). The main advantages of this method are that it invokes simple computer integer arithmetic and leads to very fast generation of sequences of random numbers with immense periods, ranging from around 260 to 22000000.In MWC base b is chosen to equal to the computer word size and multiplier a and lag r determine the modulus p = abr−1. Here, a is chosen so the modulus is prime and the multiplier has long period.AlgorithmBegin Declare maximum _sequence _elements, b, r, c[maximum _sequence ... Read More

C program to write an image in PGM format

Chandu yadav
Updated on 30-Jul-2019 22:30:25

1K+ Views

The PGM is the Portable Gray Map. If we want to store a 2d array in C as images in PNG, JPEG, or any other image format, we have to do lots of work to encode the data in some specified format before writing into a file.The Netpbm format gives an easy and portable solution. The Netpbm is an open source package of graphics program and it is used basically in linux or Unix platform. It also works under Microsoft Windows systems.Each file starts with a two-byte magic number. This magic number is used to identify the type of the ... Read More

C++ Program to Implement Naor-Reingold Pseudo Random Function

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

118 Views

Naor-Reingold Pseudo Random Function is another method of generating random numbers.Moni Naor and Omer Reingold described efficient constructions for various cryptographic primitives in private key as well as public-key cryptography, in 1997. Let p and l be prime numbers with l |p−1. Select an element g ε Fp* of multiplicative order l. Then for each n-dimensional vector a = (a0, a1, ..., an).They define the functionfa(x)=ga0.a1x1a2x2…..anxn ε Fpwhere x = x1 … xn is the bit representation of integer x, 0 ≤ x ≤ 2 n−1This function can be used as the basis of many cryptographic schemes including symmetric encryption, ... Read More

C Program to validate an IP address

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

7K+ Views

In this program we will see how to validate an IP address using C. The IPv4 addresses are represented in dot-decimal notation. There are four decimal numbers (all are ranging from 0 to 255). These four numbers are separated by three dots.An example of a valid IP is: 192.168.4.1To validate the IP address we should follow these stepsTokenize the string (IP address) using the dot “.” delimiterIf the sub strings are containing any non-numeric character, then return falseIf the number in each token is not in range 0 to 255, then return falseIf there are exactly three dots and four ... Read More

C program to print characters without using format specifiers

George John
Updated on 30-Jul-2019 22:30:25

568 Views

In this article we will see how we can print some characters without using any kind of format specifiers. The format specifiers in C are %d, %f, %c etc.These are used to print characters and numbers in C using the printf() function.Here we will see another way to print characters without using %c format specifier. This can be done by putting ASCII values directly in hexadecimal form.Example Code#include main () { printf("\x41 "); //41 is ASCII of A in Hex printf("\x52 "); //41 is ASCII of A in Hex printf("\x69 ... Read More

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

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

355 Views

Park-Miller Random Number Generation Algorithm is another method of generating random numbers.A general formula of a random number generator (RNG) of this type is: X_{k+1} = g X(k) mod nWhere 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 coprime to n.AlgorithmBegin    Declare variables n, a, b, c and seed    Read variables n, a, b, c and seed    Uniform()    Declare variable hi, lo, t    hi=seed divided by b    lo = seed - ... Read More

Write a C program that does not terminate when Ctrl+C is pressed

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

1K+ Views

In this section we will see how to write a program in C that cannot be terminated by the Ctrl + C key.The Ctrl + C generates the keyboard interrupt, and it stops the execution of the current process. Here when we will press the Ctrl + C key, it will print a message then continues the execution. To use this functionality, we will use the signal handling technique in C. When the Ctrl + C is pressed it generates SIGINT signal. There are some other signals and their functionalities in the following list.SignalDescriptionSIGABRTIndicates Abnormal terminationSIGFPE Indicates floating point exceptionSIGILL Indicates invalid ... Read More

Advertisements