Found 7347 Articles for C++

Program for Mean Absolute Deviation in C++

Sunidhi Bansal
Updated on 13-Aug-2020 07:38:04

630 Views

Given with an array of natural numbers and the task is to calculate the mean absolute deviation and for that we must require the knowledge of mean, variance and standard deviation.There are steps that need to be followed for calculating the mean absolute deviationCalculate the meanCalculate absolute deviationAdd all the calculated deviationsApply the formulaInput arr[] = { 34, 21, 56, 76, 45, 11}Output mean absolute deviation is : 18.5Input arr[] = {10, 15, 15, 17, 18, 21}Output mean absolute mean absolute deviation is : 2.66used in the given program is as followsInput the elements of an arrayCalculate the mean of an arrayCalculate deviation using ... Read More

Program for harmonic mean of numbers in C++

Sunidhi Bansal
Updated on 13-Aug-2020 07:33:54

838 Views

Given with an array of natural numbers and the task is to calculate the harmonic mean of given numbers and print it.What is harmonic mean?Harmonic mean means the reciprocal of the arithmetic mean by their reciprocals.$$Harmonic\: Mean=\frac{n}{\frac{1}{a}+\frac{1}{b}+\frac{1}{c}+...}$$Where, n is the total number of elements given and a, b, c, .. are the actual elements in an array.Steps to calculate the harmonic mean are −Do the reciprocal of the elementsAdd all the reciprocated elements togetherNow divide the total number of elements in an array by the sum of reciprocated elementsInput arr[] = {2.0, 3.4, 5.3, 2.1}Output Harmonic mean is: 2.74163Input arr[] = {13.5, 14.5, ... Read More

Program for cube sum of first n natural numbers in C++

Sunidhi Bansal
Updated on 13-Aug-2020 07:31:58

553 Views

Given an integer n, the task is to find the sum of the cube of first n natural numbers. So, we have to cube n natural numbers and sum their results.For every n the result should be 1^3 + 2^3 + 3^3 + …. + n^3. Like we have n = 4, so the result for the above problem should be: 1^3 + 2^3 + 3^3 + 4^3.Input 4Output 100Explanation 1^3 + 2^3 + 3^3 + 4^3 = 100.Input 8Output 1296Explanation 1^3 + 2^3 + 3^3 + 4^3 + 5^3 + 6^3 + 7^3 +8^3 = 1296.Approach used below is as follows to solve the problemWe ... Read More

Product of all the elements in an array divisible by a given number K in C++

Sunidhi Bansal
Updated on 13-Aug-2020 07:29:56

351 Views

Given an array arr[n] with n number of integers and another integer k, the task is to find the product all the elements of arr[] which are divisible by k.To solve the problem we have to iterate every element of the array and find whether it is completely divisible by the number k and then product all the elements and store it into a variable. Like we have an array arr[] = {1, 2, 3, 4, 5, 6 } and assuming we have k = 2 so the numbers in the array which are divisible by 2 are 2, 4, ... Read More

Product of all the Composite Numbers in an array in C++

Sunidhi Bansal
Updated on 13-Aug-2020 07:28:02

178 Views

Given an array arr[n] of n number of integers, the task is to find the product of all composite numbers in an array.Composite numbers are the whole numbers which are made by multiplying 2 other whole numbers. For example 6 is a composite number which can be made by multiplying 2 and 3 which are whole numbers. Also we can say they are not prime.Input arr[] = {1, 2, 4, 5, 6, 7}Output 24Explanation − the composite numbers in the array are 4 and 6 and their product is 24.Input arr[] = {10, 2, 4, 5, 6, 11}Output 240Explanation − the composite numbers in ... Read More

Product of all pairwise consecutive elements in an Arrays in C++

Sunidhi Bansal
Updated on 13-Aug-2020 07:25:21

272 Views

Given an array arr[n] of n number of integers, the task is to find the product of all pairwise consecutive elements.Consecutive elements in an array arr[] are, if we are at ith element, i.e. arr[i] then its consecutive element will be either arr[i+1] or arr[i-1], so the product will be arr[i] * arr[i+1] or arr[i] * arr[i-1].Input arr[] = {1, 2, 3, 4}Output 2, 6, 12Explanation Splitting into pairs {1, 2}, {2, 3}, {3, 4} Their results will be 1*2 = 2, 2*3 = 6, 3*4 = 12Input arr[] = {9, 5, 1, 2, 6, 10}Output 45, 5, 2, 12, 60Explanation Splitting into pairs {9, 5}, ... Read More

Probability that the pieces of a broken stick form a n sided polygon in C++

Sunidhi Bansal
Updated on 13-Aug-2020 07:23:38

95 Views

We are given with the stick of any length and that stick can be broken randomly into n pieces which can be of type integer or floating point and the task is to find whether the broken pieces can form a n sided polygon.We can calculate the probability by applying the formula$$P(E^{\prime})=1-P(E)=1-\frac{n}{2^{n-1}}$$Where, n is the number of pieces generated by breaking the stick into parts.Input length = 10 , pieces = 4Output probability is : 0.5Explanation − given with length of size 10 cm and it is broken into 4 partsInput length = 5 , pieces = 3Output probability is : 0.25Explanation − given ... Read More

Probability of reaching a point with 2 or 3 steps at a time in C++

Sunidhi Bansal
Updated on 13-Aug-2020 07:22:16

186 Views

A person “A” is walking from a starting position X = 0, the task is to find the probability to reach exactly X = num, if he/she can either take 2 or 3 steps. Probability for step length 2 i.e. P, the probability for the step length 3 is 1 - P.Input num = 5, p = 0.2Output 0.32Explanation There can be 2 ways to reach num, i.e, 5 2+3 with probability 0.2 * 0.8 = 0.16 3+2 with probability 0.8 * 0.2 = 0.16 So, total probability will be 0.16 + 0.16 = 0.32Input num = 2, p = 0.1Output 0.1Approach used below is ... Read More

Probability of rain on N+1th day in C++

Sunidhi Bansal
Updated on 13-Aug-2020 07:20:33

100 Views

Given with an array containing 0’s and 1’s where 0’s represents no rain and 1’s represent rainy day. The task is to calculate the probability of rain on N+1th day.To calculate the probability of rain on N+1th day we can apply the formulaTotal number of rainy days in the set / total number of days in aInput arr[] = {1, 0, 0, 0, 1 }Output probability of rain on n+1th day : 0.4Explanation total number of rainy and non-rainy days are: 5 Total number of rainy days represented by 1 are: 2 Probability of rain on N+1th day is: 2 / 5 = ... Read More

Probability of getting more value in third dice throw in C++

Sunidhi Bansal
Updated on 13-Aug-2020 07:18:10

66 Views

Given three players A, B, C throwing dice, we have to find the probability of the C throwing the dice and the number scored by C is higher than both A and B.To check the probability of getting more value, we have to keep in mind that the value of the third dice throw is higher than the previous two.Like A thrown the dice and score 2 and B thrown the dice and scored 3 so the probability of C getting higher value is 3/6 = 1/2, because there are only 3 values which can be higher than the A ... Read More

Advertisements