Found 34484 Articles for Programming

C++ Program to Implement Booth’s Multiplication Algorithm for Multiplication of 2 signed Numbers

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

2K+ Views

Booth’s algorithm is a multiplication algorithm that multiplies two signed binary numbers in 2’s compliment notation. Booth used desk calculators that were faster at shifting than adding and created the algorithm to increase their speed.AlgorithmBegin    Put multiplicand in BR and multiplier in QR       and then the algorithm works as per the following conditions:    1. If Qn and Qn+1 are same i.e. 00 or 11 perform arithmetic shift by 1 bit.    2. If Qn Qn+1 = 10 do A= A + BR and perform arithmetic shift by 1 bit.    3. If Qn Qn+1 = ... Read More

C++ Program to Perform Fermat Primality Test

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

702 Views

Fermat Primality test performs to check a given number is prime or not. Here is a C++ code of this algorithm.AlgorithmBegin    modulo(base, e, mod)    a = 1    b = base    while (e > 0)       if (e mod 2 == 1)          a = (a * b) % mod          b = (b * b) % mod          e = e / 2       return a % mod End Begin    Fermat(ll m, int iterations)    if (m == 1)     ... Read More

C++ Program to Generate Prime Numbers Between a Given Range Using the Sieve of Sundaram

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

385 Views

This is C++ program to implement Sieve of Sundaram to Generate Prime Numbers Between Given Range. This algorithm was discovered in 1934 by Sundaram.AlgorithmBegin printPrimes(n) Here we find out primes smaller than n, we reduce n-2 to half. We call it New. New = (n-2)/2; Create an array marked[n] that is going to be used to separate numbers of the form i+j+2ij from others where 1

C++ Program to Find the GCD and LCM of n Numbers

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

3K+ Views

This is the code to find out the GCD and LCM of n numbers. The GCD or Greatest Common Divisor of two or more integers, which are not all zero, is the largest positive integer that divides each of the integers. GCD is also known as Greatest Common Factor.The least common multiple (LCM) of two numbers is the smallest number (not zero) that is a multiple of both numbers.AlgorithmBegin    Take two numbers as input    Call the function gcd() two find out gcd of n numbers    Call the function lcm() two find out lcm of n numbers   ... Read More

C++ Program to Implement the Rabin-Miller Primality Test to Check if a Given Number is Prime

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

893 Views

Rabin-Miller Primality Test is used to check if a given Number is Prime or not. It is similar to the format primality and the Solovay-Stressen test. this test first was discovered by Russian Mathematician M. M. Artjuhov.AlgorithmBegin    ll mulmod(ll a, ll b, ll m)    ll x = 0, y = a mod m    while (b > 0)       if (b mod 2 == 1)          compute x = (x + y) mod m          y = (y * 2) mod m          b = b/ 2 ... Read More

What is the difference between #include and #include “filename”?

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

264 Views

The difference between the two forms is in the location where the preprocessor searches for the file to be included.#include The preprocessor searches in an implementation-dependent manner, it searches directories pre-designated by the compiler. This method is usually used to include standard library header files.#include "filename"The preprocessor searches in the same directory as the file containing the directive. If this fails, then it starts behaving like the #include form. This method is usually used to include your own header files.

Why should I not #include ?

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

107 Views

The is a header file. This file includes all standard library. Sometimes in some coding contests, when we have to save time while solving, then using this header file is helpful.In software engineering approach we should reduce the minimize the include. Using this header file, it will include lots of files, sometimes that may not be required in the program. So it may increase the compile time and program size.Some of the big disadvantages of this header file is listed belowThis is not a standard header file of GNU C++ library. So some compiler may fail to compiler ... Read More

When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used?

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

295 Views

const_castcan be used to remove or add const to a variable. This can be useful if it is necessary to add/remove constness from a variable.static_castThis is used for the normal/ordinary type conversion. This is also the cast responsible for implicit type coercion and can also be called explicitly. You should use it in cases like converting float to int, char to int, etc.dynamic_castThis cast is used for handling polymorphism. You only need to use it when you're casting to a derived class. This is exclusively to be used in inheritance when you cast from base class to derived class.reinterpret_castThis is ... Read More

Why does the order in which libraries are linked sometimes cause errors in GCC?

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

115 Views

Basically this kind of errors are originated from the linker in the compilation phase. The default behavior of a linker is to take the code from archive libraries when the current program needs it.To work properly the libraries must be present in order. We can say that it must be there in the form “caller before callees”. This problem can be solved by choosing non-default behavior using flags, but in this process the linking may take larger time. Otherwise it can be solved by ordering the libraries correctly. Loaders and tsort these two can help to rearrange and correct the ... Read More

Where do I find the current C or C++ standard documents?

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

215 Views

In this post you can get some details where you can buy and view free drafts of some current and past C/C++ standards.C DocumentsC11:198 CHF (https://www.iso.org/standard/57853.html)Publicly at (http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1570.pdf)Wikipedia Link (https://en.wikipedia.org/wiki/C11_(C_standard_revision))C99:Cannot Purchase (https://www.iso.org/standard/29237.html)Publicly at (http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf)Wikipedia Link (https://en.wikipedia.org/wiki/C99)C90, C89, ANSI C, Standard CWikipedia Page: (https://en.wikipedia.org/wiki/ANSI_C)C++ DocumentsC++14:198 CHF (https://www.iso.org/standard/64029.html)Based On (https://www.iso.org/standard/29237.html)Wikipedia Link (https://en.wikipedia.org/wiki/ANSI_C)C++11:Cannot Purchase (https://www.iso.org/standard/50372.html)Publicly at (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf)Wikipedia Link (https://en.wikipedia.org/wiki/C%2B%2B11)C++03Wikipedia Page: (https://en.wikipedia.org/wiki/C%2B%2B03)C++89Wikipedia Page: (https://en.wikipedia.org/wiki/C%2B%2B)Read More

Advertisements