Found 7347 Articles for C++

Arithmetic Mean in c++?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

1K+ Views

The Arithmetic Mean is just the average of numbers. In this program we will see how we can find the arithmetic mean from a set of numbers. The function will take the number set, and the number of elements. Out task is just adding each element, then divide it by number of elements that are passed.AlgorithmarithmeticMean(dataset, n)begin    sum := 0    for each element e from dataset, do       sum := sum + e    done    return sum/n endExample Live Demo#include using namespace std; float arithmetic_mean(float data[], int size) {    float sum = 0;    for(int i = 0; i

Alternate Primes till N in C++?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

955 Views

Here we will see how to print all alternate prime numbers till N. The alternate prime numbers are like below. Suppose N = 15. So the prime numbers till N are {2, 3, 5, 7, 11, 13}. The alternate prime numbers are {2, 5, 11}. Let us see how we can solve this problem.AlgorithmprintAlternatePrime(N)Begin    define one Boolean array prime of size N + 1, and fill with 1.    for p := 2, p^2 is less than N, increase p by 1, do       if prime[p] is true, then          for all multiples of ... Read More

Aliquot sum in C++?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

184 Views

Here we will see what is the Aliquot sum? The Aliquot sum of n is the sum of all perfect factors of a n except the n. For example, if the number is 20, then the perfect factors are (1, 2, 4, 5, 10). So the Aliquot sum is 22.One interesting fact is that, if Aliquot sum of a number is the number itself, then that number is a perfect number. For example, 6. The factors are (1, 2, 3). The Aliquot sum is 1+2+3=6.Let us see how we can get the Aliquot sum using the following algorithm.AlgorithmgetAliquotSum(n)begin    sum ... Read More

C/C++ program to shutdown a system?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

524 Views

Here we will see how we can shut down the system by writing a simple C or C++ code. The shutdown process varies in different OS. If we are Linux user, we can use this terminal command to shut down.shutdown –P nowIf we are using Windows system, we can use this command −c:\windows\system32\shutdown /iWe will see the code for Linux and WindowsExample(Linux)#include using namespace std; int main() {    system("shutdown -P now"); }Example(Windows)#include using namespace std; int main() {    system("c:\windows\system32\shutdown /i "); }

C/C++ Program to Find the Number Occurring Odd Number of Times?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

104 Views

In this program we will see how we can get a number that is occurring odd number of times in an array. There are many different approaches. One of the easiest approach is performing ZOR operation. If a number is XORed with itself, it will be 0. So if a number XORed even number of times, it will be 0, otherwise the number itself.This solution has one problem, if more than one element has odd number of occurrences, it will return one of them.AlgorithmgetNumOccurredOdd(arr, n)begin    res := 0    for each element e from arr, do       ... Read More

C/C++ Program to Count set bits in an integer?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

900 Views

Here we will see how we can check number of set bits in an integer number. The set bits are 1’s in the binary representation of a number. For an example the number 13 has three set bits 1101. So the count will be 3.To solve this problem, we will shift the number to the right, and if the LSb is 1, then increase count. Until the number becomes 0, it will run.AlgorithmcountSetBit()begin    count := 0    while count is not 0, do       if LSb of n is set, then          count := ... Read More

C/C++ Program to Count Inversions in an array using Merge Sort?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

640 Views

The inversions of an array indicate; how many changes are required to convert the array into its sorted form. When an array is already sorted, it needs 0 inversions, and in other case, the number of inversions will be maximum, if the array is reversed.To solve this problem, we will follow the Merge sort approach to reduce the time complexity, and make it in Divide and Conquer algorithm.InputA sequence of numbers. (1, 5, 6, 4, 20).OutputThe number of inversions required to arrange the numbers into ascending order.Here the number of inversions are 2. First inversion: (1, 5, 4, 6, 20) ... Read More

C/C++ Program for Triangular Matchstick Number?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

87 Views

Here we will see how to count number of matchsticks are required to make the pyramid-like below. The base of the pyramid is given. So if the base is 1, it will take 3 matchsticks to make a pyramid, for base 2, 9 matchsticks are needed, for base size 3, it will take 18 matchsticks.To solve this problem, we have to use this formula −Example Live Demo#include using namespace std; int main(){    int x;    cout > x;    int count = 3*x*(x+1)/2;    cout

C/C++ Program for Maximum height when coins are arranged in a triangle?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

528 Views

In this section, we will see one interesting problem. There are N coins. we have to find what is the max height we can make if we arrange the coins as pyramid. In this fashion, the first row will hold 1 coin, second will hold 2 coins and so on.In the given diagram, we can see to make a pyramid of height three we need minimum 6 coins. We cannot make height 4 until we have 10 coins. Now let us see how to check the maximum height.We can get the height by using this formula.Example Live Demo#include #include using namespace ... Read More

c32rtomb() function in C/C++?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

93 Views

In C++, we can use 32-bit character representations. The c32rtomb() function is used to convert 32-bit character representation to narrow multi-byte character representation. We can find this function inside the uchar.h header file.This function takes three parameters. These are −The string where multi-byte character will be stored32-bit character to convertThe pointer of type mbstate_t object. which is used to interpret multibyte string.This function returns number of bytes written to the character array, when it is successful, otherwise returns -1. Let us see an example to get better idea.Example Live Demo#include #include #include using namespace std; int main() { ... Read More

Advertisements