Found 26504 Articles for Server Side Programming

Multiply a given Integer with 3.5 in C++

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

398 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

1K+ 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

643 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

861 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

Move all zeros to the front of the linked list in C++

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

363 Views

Given a linked list with random integers and zeroes. We have to move all the zeroes to the front of the linked list. Let's see an example.Input3 -> 0 -> 1-> 0 -> 0 -> 1 -> 0 -> 0 -> 3 -> NULLOutput0->0->0->0->0->3->1->1->3->NULL AlgorithmInitialise the linked list.Return if the linked list is empty or it has single node.Initialise two nodes with second node and first node respectively to track current and previous nodes.Iterate over the linked list until we reach the end.If the current node is 0, then make it new head.Update the values of current and previous nodes ... Read More

Move all zeros to start and ones to end in an Array of random integers in C++

Hafeezul Kareem
Updated on 25-Oct-2021 04:49:34

834 Views

In this tutorial, we are going to write a program that moves all zeroes to front and ones to end of the array.Given an array with zeroes and ones along with random integers. We have to move all the zeroes to start and ones to the end of the array. Let's see an example.Inputarr = [4, 5, 1, 1, 0, 0, 2, 0, 3, 1, 0, 1]Output0 0 0 0 4 5 2 3 1 1 1 1AlgorithmInitialise the array.Initialise an index to 1.Iterate over the given array.If the current element is not zero, then update the value at the ... Read More

Move all zeroes to end of array in C++

Hafeezul Kareem
Updated on 25-Oct-2021 04:44:33

3K+ Views

Given array with multiple zeroes in it. We have to move all the zeroes in the array to the end. Let's see an example.Inputarr = [4, 5, 0, 3, 2, 0, 0, 0, 5, 0, 1]Output4 5 3 2 5 1 0 0 0 0 0AlgorithmInitialise the array.Initialise an index to 0.Iterate over the given array.If the current element is not zero, then update the value at the index with the current element.Increment the index.Write a loop that iterates from the above index to nUpdate all the elements to 0.ImplementationFollowing is the implementation of the above algorithm in C++#include ... Read More

Motzkin number in C++

Hafeezul Kareem
Updated on 25-Oct-2021 04:40:19

209 Views

The Motzkin number series starts with 1, 1, 4, 9, etc.., We can get the generalised nth term with the sequence. The Motzkin number sequence is as follows.a0 = 1a1 = 1a2 = 4a3 = 9an = ((2 * n + 1)/ n + 2) * M(n-1) +((3 * n - 3)/ n + 2) * M(n - 2)AlgorithmInitialise the number n.Iterate till n.Update the previous two numbersReturn the last number.ExampleImplementationFollowing is the implementation of the above algorithm in C++#include using namespace std; int getNthTerm(int n) {    if(n == 0 || n == 1) {       return 1;    }    int a = 1, b = 1;    for(int i = 2; i

Next smallest prime palindrome in C++

Hafeezul Kareem
Updated on 25-Oct-2021 04:27:23

237 Views

We are given a number N. We need to find the prime palindrome that is greater than N. Let's see an example.InputN = 10Output11AlgorithmInitialise the number N.Write a function to check whether the given number is prime or not.Write a function to check whether the given number is palindrome.Write a loop that iterates from N + 1 till you find the next prime palindrome.Check if the number is prime and palindrome.If the number is prime and palindrome.Return the number.ImplementationFollowing is the implementation of the above algorithm in C++#include using namespace std; bool isPrime(int n) {    if (n < 2) ... Read More

Advertisements