Found 7347 Articles for C++

Find N Arithmetic Means between A and B using C++.

Arnab Chakraborty
Updated on 30-Oct-2019 06:03:19

68 Views

Suppose we have three integers A, B and N. We have to find N arithmetic means between A and B. If A = 20, B = 32, and N = 5, then the output will be 22, 24, 26, 28, 30The task is simple we have to insert N number of elements in the Arithmetic Progression where A and B are the first and last term of that sequence. Suppose A1, A2, …. An are n arithmetic means. So the sequence will be A, A1, A2, …. An, B. So B is the (N + 2)th term of the sequence. ... Read More

Find minimum sum of factors of number using C++.

Arnab Chakraborty
Updated on 30-Oct-2019 06:01:24

282 Views

Here we will see how to get a minimum sum of factors of a given number. Suppose a number is 12. We can factorize this in different ways −12 = 12 * 1 (12 + 1 = 13)12 = 2 * 6 (2 + 6 = 8)12 = 3 * 4 (3 + 4 = 7)12 = 2 * 2 * 3 (2 + 2 + 3 = 7)The minimum sum is 7. We will take a number, and try to find the minimum factor sum. To get the minimum factor sum, we have to factorize the number as long ... Read More

Find max in struct array using C++.

Arnab Chakraborty
Updated on 30-Oct-2019 05:58:55

540 Views

Here we will see how to get max in the struct array. Suppose there is a struct like below is given. We have to find the max element of an array of that struct type.struct Height{    int feet, inch; };The idea is straight forward. We will traverse the array, and keep track of the max value of array element in inches. Where value is 12*feet + inchExample#include #include using namespace std; struct Height{    int feet, inch; }; int maxHeight(Height h_arr[], int n){    int index = 0;    int height = INT_MIN;    for(int i = 0; i ... Read More

Find last two digits of sum of N factorials using C++.

Arnab Chakraborty
Updated on 30-Oct-2019 05:57:05

105 Views

Here we will see how to get the last two digits. The unit place digit and the tens place digit of the sum of N factorials. So if N = 4, it will be 1! + 2! + 3! + 4! = 33. so unit place is 3 and ten place is 3. The result will be 33.If we see this clearly, then as the factorials of N > 5, the unit place is 0, so after 5, it will not contribute to change the unit place. And after N > 10, the ten places will remain 0. For N ... Read More

Find largest prime factor of a number using C++.

Arnab Chakraborty
Updated on 30-Oct-2019 05:53:55

1K+ Views

Consider we have an element x, we have to find the largest prime factor of x. If the value of x is 6,  then-largest prime factor is 3. To solve this problem, we will just factorize the number by dividing it with the divisor of a number and keep track of the maximum prime factor.Example Live Demo#include #include using namespace std; long long getMaxPrimefactor(long long n) {    long long maxPF = -1;    while (n % 2 == 0) {       maxPF = 2;       n /= 2;    }    for (int i = 3; i 2)    maxPF = n;    return maxPF; } int main() {    long long n = 162378;    cout

Find if two rectangles overlap using C++.

Arnab Chakraborty
Updated on 30-Oct-2019 05:51:34

753 Views

We know that a rectangle can be represented using two coordinates, the top left corner, and the bottom right corner. Suppose there are two rectangles, we have to check whether these two overlap or not. There are four coordinate points (l1, r1) and (l2, r2).l1 is the top-left corner of first rectangler1 is the bottom-right corner of the first rectanglel2 is the top-left corner of second rectangler2 is the bottom-right corner of the second rectangleWe have assumed that the rectangles are parallel to the coordinate axes. To solve this, we have to check a few conditions.One rectangle is above the ... Read More

Find if a number is part of AP whose first element and difference are given using C++.

Arnab Chakraborty
Updated on 30-Oct-2019 05:47:45

468 Views

Suppose we have the first element of AP, and the differenced. We have to check whether the given number n is a part of AP or not. If the first term is a = 1, differenced = 3, and the term x = 7 will be checked. The answer is yes.To solve this problem, we will follow these steps −If d is 0, and a = x, then return true, otherwise false.Otherwise, if d is not 0, then if x belongs to the sequence x = a + n * d, where n is a non-negative integer, only if (n ... Read More

Find Harmonic mean using Arithmetic mean and Geometric mean using C++.

Arnab Chakraborty
Updated on 30-Oct-2019 05:44:56

249 Views

Here we will see how to get the Harmonic mean using the arithmetic mean and the geometric mean. The formula for these three means are like below −Arithmetic Mean − (a + b)/2Geometric Mean − $$\sqrt{\lgroup a*b\rgroup}$$Harmonic Mean − 2ab/(a+b)The Harmonic Mean can be expressed using arithmetic mean and geometric mean using this formula −$$HM=\frac{GM^{2}}{AM}$$Example Live Demo#include #include using namespace std; double getHarmonicMean(int a, int b) {    double AM, GM, HM;    AM = (a + b) / 2;    GM = sqrt(a * b);    HM = (GM * GM) / AM;    return HM; } int ... Read More

Find floor and ceil in an unsorted array using C++.

Arnab Chakraborty
Updated on 29-Oct-2019 12:03:00

480 Views

Here we will see how to find the floor and ceiling in an unsorted array. The floor value is larger element which is smaller than or equal to x, and the ceiling value is smallest value which is larger than x. If the array A = [5, 6, 8, 9, 6, 5, 5, 6], and x is 7, then the floor value is 6, and the ceiling value is 8.To solve this problem, we will follow the linear search approach. We will traverse the array and track two distances with respect to x.Min distance of element greater than or equal ... Read More

Find difference between sums of two diagonals in C++.

Arnab Chakraborty
Updated on 29-Oct-2019 11:54:13

394 Views

Here we will see how to get the difference between the sums of two diagonals of a given matrix. Suppose we have a matrix of order N x N, we have to get the sum of primary and secondary diagonals, then get the difference of them. To get the major diagonal, we know that the row index and column index increases simultaneously. For the second diagonal, row index and column index values are increased by this formula row_index = n – 1 – col_index. After getting the sum, take the difference and return a result.Example Live Demo#include #include #define MAX 100 ... Read More

Advertisements