Found 7347 Articles for C++

Multiply two polynomials in C++

Hafeezul Kareem
Updated on 26-Oct-2021 05:56:45

3K+ Views

The coefficients of each term of the polynomial are given in an array. We need to multiply the two polynomials. Let's see an example.InputA = [1, 2, 3, 4] B = [4, 3, 2, 1]Output4x6 + 11x5 + 20x4 + 30x3 + 20x2 + 11x1 + 4AlgorithmInitialise two polynomials.Create a new array with a length of two polynomials.Iterate over the two polynomials.Take one term from the first polynomial and multiply it with all the terms in the second polynomial.Store the result in the resultant polynomial.ImplementationFollowing is the implementation of the above algorithm in C++#include using namespace std; int *multiplyTwoPolynomials(int ... Read More

Multiply two numbers represented by Linked Lists in C++

Hafeezul Kareem
Updated on 25-Oct-2021 16:28:34

887 Views

Given two linked lists with digits in it. We need to multiply two numbers formed by the linked list. It can be done easily by forming the numbers from the two linked lists. Let's see an example.Input1 -> 2 -> NULL 2 -> 3 -> NULLOutput2 -> 7 -> 6 -> NULLAlgorithmInitialise the two linked lists.Initialise two variables with 0 to store the two numbers.Iterate over the two linked lists.Add each digit to the respective number variable at the end.Multiply the resultant numbers and store the result in a variable.Create a new list with the result.Print the new list.ImplementationFollowing is ... Read More

Multiply two numbers represented as linked lists into a third list in C++

Hafeezul Kareem
Updated on 25-Oct-2021 17:04:14

174 Views

Given two linked lists with digits in it. We need to multiply two numbers formed by the linked list. It can be done easily by forming the numbers from the two linked lists. Let's see an example.Input1 -> 2 -> NULL 2 -> 3 -> NULLOutput2 -> 7 -> 6 -> NULLAlgorithmInitialise the two linked lists.Initialise two variables with 0 to store the two numbers.Iterate over the two linked lists.Add each digit to the respective number variable at the end.Multiply the resultant numbers and store the result in a variable.Create a new list with the result.Print the new list.ImplementationFollowing is ... Read More

Multiply Large Numbers represented as Strings in C++

Hafeezul Kareem
Updated on 25-Oct-2021 07:10:44

3K+ Views

Given two numbers in the string formats. We need to multiply them. The idea to solve the problem is to maintain a previous digit multiplication answer and carry. We can use the previous digits multiplication answer and carry to get the next set digits multiplication.Let's see an example.Input15 2Output30AlgorithmInitialise the numbers in string.Initialise a string of length number_one_length + number_two_length.Iterate over the first number from the end.Iterate over the second number from the end.Multiply two digits and add the corresponding previous row digit.Update the previous row digit.Store the carry in the previous index of the result string.Convert the char to ... Read More

Multiply any Number with using Bitwise Operator in C++

Hafeezul Kareem
Updated on 25-Oct-2021 06:31:30

9K+ Views

In this tutorial, we are going write a program that multiplies the given two numbers using bitwise operators.The left shift () is used for the division.The multiplication of two numbers x, y can be written as x * y = (x * 2) * (y / 2) if y is even else it's equal to x * y = (x * y) * (y / 2) + x.So whenever the second number becomes odd, add the first number to the result. Let's see the steps to solve the problem.AlgorithmInitialise two numbers.Write a loop that iterates till the second number becomes ... Read More

Multiply a given Integer with 3.5 in C++

Hafeezul Kareem
Updated on 25-Oct-2021 05:48:06

274 Views

To get the result of n * 3.5 we need to calculate (n * 2) + n + (n / 2). Moving the bits to left by 1 will give you n * 2 and moving the bits to right by will you n / 2. Add those to get the result.n * 3.5 = (n * 2) + n + (n / 2)You can submit different values of n to verify the above equation. Let's see some examples.Input2 7 10Output7 24 35AlgorithmInitialise the number n.Find the n * 2 using left shift bitwise operatorFind the n / 2 using ... Read More

Multiples of 3 or 7 in C++

Hafeezul Kareem
Updated on 25-Oct-2021 05:40:28

919 Views

Given a number n, we need to find the count of multiples of 3 or 7 till n. Let's see an example.Input100Output43There are total of 43 multiples of 3 or 7 till 100.AlgorithmInitialise the number n.Initialise the count to 0.Write a loop that iterates from 3 to n.Increment the count if the current number is divisible by 3 or 7.ImplementationFollowing is the implementation of the above algorithm in C++#include using namespace std; int getMultiplesCount(int n) {    int count = 0;    for (int i = 3; i

Multiples of 3 and 5 without using % operator in C++

Hafeezul Kareem
Updated on 27-Oct-2021 06:28:22

431 Views

We can find the multiples using % operator without any hurdles. But, the problem states that we can't use % operator.Here, we make use of the + operator. We can get the multiples by adding 3 or 5 to the previous multiple. Let's see an example.Input15Output1 2 3 - Multiple of 3 4 5 - Multiple of 5 6 - Multiple of 3 7 8 9 - Multiple 3 10 - Multiple of 5 11 12 - Multiple of 3 13 14 15 - Multiple of both 3 and 5AlgorithmInitialise the number n.Initialise two number to keep track of next ... Read More

Move last element to front of a given Linked List in C++

Hafeezul Kareem
Updated on 25-Oct-2021 05:21:53

704 Views

Given a linked list, we have to move the last element to the front. Let's see an example.Input1 -> 2 -> 3 -> 4 -> 5 -> NULLOutput5 -> 1 -> 2 -> 3 -> 4 -> NULLAlgorithmInitialise the linked list.Return if the linked list is empty or it has single node.Find the last node and second last nodes of the linked list.Make the last node as new head.Update the link of second last node.ImplementationFollowing is the implementation of the above algorithm in C++#include using namespace std; struct Node {    int data;    struct Node* next; }; void ... Read More

Advertisements