Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes

C++ Programming Code Examples

C++ > Mathematics Code Examples

Implement Booth's Multiplication Algorithm for Multiplication of 2 signed Numbers

/* Implement Booth's Multiplication Algorithm for Multiplication of 2 signed Numbers This is a C++ Program to multiply two signed numbers using booth's algorithm. Booth's multiplication algorithm is a multiplication algorithm that multiplies two signed binary numbers in two's complement notation. Booth used desk calculators that were faster at shifting than adding and created the algorithm to increase their speed. Booth's algorithm is of interest in the study of computer architecture. */ #include<iostream> #include<conio.h> using namespace std; void add(int a[], int x[], int qrn); void complement(int a[], int n) { int j; int x[8] = { NULL }; x[0] = 1; for (j = 0; j < n; j++) { a[j] = (a[j] + 1) % 2; } add(a, x, n); } void add(int ac[], int x[], int qrn) { int j, c = 0; for (j = 0; j < qrn; j++) { ac[j] = ac[j] + x[j] + c; if (ac[j] > 1) { ac[j] = ac[j] % 2; c = 1; } else c = 0; } } void ashr(int ac[], int qr[], int &qn, int qrn) { int temp, j; temp = ac[0]; qn = qr[0]; cout << "\t\tashr\t\t"; for (j = 0; j < qrn - 1; j++) { ac[j] = ac[j + 1]; qr[j] = qr[j + 1]; } qr[qrn - 1] = temp; } void display(int ac[], int qr[], int qrn) { int j; for (j = qrn - 1; j >= 0; j--) cout << ac[j]; cout << " "; for (j = qrn - 1; j >= 0; j--) cout << qr[j]; } int main(int argc, char **argv) { int mt[10], br[10], qr[10], sc, ac[10] = { 0 }; int brn, qrn, j, qn, temp; cout << "\n--Enter the multiplicand and multipier in signed 2's complement form if negative--"; cout << "\n Number of multiplicand bit="; cin >> brn; cout << "\nmultiplicand="; for (j = brn - 1; j >= 0; j--) cin >> br[j]; //multiplicand for (j = brn - 1; j >= 0; j--) mt[j] = br[j]; // copy multipier to temp array mt[] complement(mt, brn); cout << "\nNo. of multiplier bit="; cin >> qrn; sc = qrn; //sequence counter cout << "Multiplier="; for (j = qrn - 1; j >= 0; j--) cin >> qr[j]; //multiplier qn = 0; temp = 0; cout << "qn\tq[n+1]\t\tBR\t\tAC\tQR\t\tsc\n"; cout << "\t\t\tinitial\t\t"; display(ac, qr, qrn); cout << "\t\t" << sc << "\n"; while (sc != 0) { cout << qr[0] << "\t" << qn; if ((qn + qr[0]) == 1) { if (temp == 0) { add(ac, mt, qrn); cout << "\t\tsubtracting BR\t"; for (j = qrn - 1; j >= 0; j--) cout << ac[j]; temp = 1; } else if (temp == 1) { add(ac, br, qrn); cout << "\t\tadding BR\t"; for (j = qrn - 1; j >= 0; j--) cout << ac[j]; temp = 0; } cout << "\n\t"; ashr(ac, qr, qn, qrn); } else if (qn - qr[0] == 0) ashr(ac, qr, qn, qrn); display(ac, qr, qrn); cout << "\t"; sc--; cout << "\t" << sc << "\n"; } cout << "Result="; display(ac, qr, qrn); }

Arithmetic Operator is used to performing mathematical operations such as addition, subtraction, multiplication, division, modulus, etc., on the given operands. For example: 6 + 3 = 9, 5 - 3 = 2, 3 * 4 = 12, etc. are the examples of arithmetic operators. Let's discuss the different types of Arithmetic Operators in the C programming. Plus Operator is a simple Plus (+) Operator used to add two given operands. We can use Plus Operator with different data types such as integer, float, long, double, enumerated and string type data to add the given operand. The minus operator is denoted by the minus (-) symbol. It is used to return the subtraction of the first number from the second number. The data type of the given number can be different types, such as int, float, double, long double, etc., in the programing language.

A program shall contain a global function named main, which is the designated start of the program in hosted environment. main() function is the entry point of any C++ program. It is the point at which execution of program is started. When a C++ program is executed, the execution control goes directly to the main() function. Every C++ program have a main() function.

The cin object is used to accept input from the standard input device i.e. keyboard. It is defined in the iostream header file. C++ cin statement is the instance of the class istream and is used to read input from the standard input device which is usually a keyboard. The extraction operator(>>) is used along with the object cin for reading inputs. The extraction operator extracts the data from the object cin which is entered using the keyboard. The "c" in cin refers to "character" and "in" means "input". Hence cin means "character input". The cin object is used along with the extraction operator >> in order to receive a stream of characters.

Consider a situation, when we have two persons with the same name, jhon, in the same class. Whenever we need to differentiate them definitely we would have to use some additional information along with their name, like either the area, if they live in different area or their mother's or father's name, etc. Same situation can arise in your C++ applications. For example, you might be writing some code that has a function called xyz() and there is another library available which is also having same function xyz(). Now the compiler has no way of knowing which version of xyz() function you are referring to within your code.

In while loop, condition is evaluated first and if it returns true then the statements inside while loop execute, this happens repeatedly until the condition returns false. When condition returns false, the control comes out of loop and jumps to the next statement in the program after while loop. The important point to note when using while loop is that we need to use increment or decrement statement inside while loop so that the loop variable gets changed on each iteration, and at some point condition returns false. This way we can end the execution of while loop otherwise the loop would execute indefinitely. A while loop that never stops is said to be the infinite while loop, when we give the condition in such a way so that it never returns false, then the loops becomes infinite and repeats itself indefinitely.

In computer programming, loops are used to repeat a block of code. For example, when you are displaying number from 1 to 100 you may want set the value of a variable to 1 and display it 100 times, increasing its value by 1 on each loop iteration. When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop. A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program.

In computer programming, we use the if statement to run a block code only when a certain condition is met. An if statement can be followed by an optional else statement, which executes when the boolean expression is false. There are three forms of if...else statements in C++: • if statement, • if...else statement, • if...else if...else statement, The if statement evaluates the condition inside the parentheses ( ). If the condition evaluates to true, the code inside the body of if is executed. If the condition evaluates to false, the code inside the body of if is skipped.

The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities. The if...else ladder allows you to check between multiple test expressions and execute different statements. In C/C++ if-else-if ladder helps user decide from among multiple options. The C/C++ if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the C else-if ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.

An array is defined as the collection of similar type of data items stored at contiguous memory locations. Arrays are the derived data type in C++ programming language which can store the primitive type of data such as int, char, double, float, etc. It also has the capability to store the collection of derived data types, such as pointers, structure, etc. The array is the simplest data structure where each data element can be randomly accessed by using its index number. C++ array is beneficial if you have to store similar elements. For example, if we want to store the marks of a student in 6 subjects, then we don't need to define different variables for the marks in the different subject. Instead of that, we can define an array which can store the marks in each subject at the contiguous memory locations.

To check whether the input alphabet is vowel or not a vowel in C++, Enter a Character, then check the character for Vowel. The character is vowel, only if it's equal to a, A, e, E, i, I, o, O