In this problem, we are given a number n which defines the nth term of the series 2 + (2+4) + (2+4+6) + (2+4+6+8) + ... + (2+4+6+8+...+2n). Our task is to create a program to find the sum of the series.Let’s take an example to understand the problem, Input n = 3OutputExplanation − sum = (2) + (2+4) + (2+4+6) = 2 + 6 + 12 = 20A simple solution to the problem is to use a nested loop. The inner loop finds the ith element of the series and then add up all elements to the sum variable.ExampleProgram to illustrate ... Read More
In this problem, we are given a number n which is the nth term of the series 1/(1*2) + 1/(2*3) +…+ 1/(n*(n+1)). Our task is to create a program to find the sum of the series.Let’s take an example to understand the problem, Input n = 3Output 0.75Explanation − sum = 1/(1*2) + 1/(2*3) + 1/(3*4) = ½ + ⅙+ 1/12 = (6+2+1)/12 = 9/12 = ¾ = 0.75A simple solution to the problem is using the loop. And commuting value for each element of the series. Then add them to the sum value.AlgorithmInitialize sum = 0 Step 1: Iterate from i = ... Read More
In this problem, we are given a number n which is given the n of elements of the series 1, 3, 6, 10 … (triangular number). Our task is to create a program to calculate the sum of the series.Let’s brush up about triangular numbers before calculating the sum.Triangular numbers are those numbers that can be represented in the form of a triangle.A triangle is formed in such a way that the first row has one point, second has two, and so on.ExampleLet’s take an example to understand the problem, Inputn = 4OutputExplanation − sum = T1 + T2 + T3 ... Read More
In this problem, we are given an integer n. Our task is to create a program to find the sum of the series 1 + (1+3) + (1+3+5) + (1+3+5+7) + + (1+3+5+7+....+(2n-1)).From this series, we can observe that ith term of the series is the sum of first i odd numbers.Let’s take an example to understand the problem, Inputn = 3Output 14Explanation −(1) + (1+3) + (1+3+5) = 14A simple solution to this problem is using a nested loop and then add all odd numbers to a sum variable. Then return the sum.ExampleProgram to illustrate the working of our solution, ... Read More
In this problem, we are given n terms of a number. The series is 0.7, 0.77, 0.777…. Our task is to create a program to find the sim of the series 0.7, 0.77, 0.777 … upto n terms.Let’s take an example to understand the problem, Input 4Output Explanation −0.7 + 0.77 + 0.777 + 0.7777 = 3.0247To solve this problem, we will derive the formula for sum of series. Let’s find the general formula for it, sum = 0.7 + 0.77 + 0.777 + ... upto n terms sum = 7 (0.1 + 0.11 + 0.111 + … upto n ... Read More
In this problem, we are given an array arr[] of N numbers. Our task is to create a program that will find the sum of the products of all possible subsets.Here, we will find all subsets and then find the product of all elements for each subset. Then add all the values to calculate the sum.Let’s take an example to understand the problem, Inputarr[] = {4, 5, 6}Output209Explanation −All subsets of arr[] are: {4}, {5}, {6}, {4, 5}, {5, 6}, {4, 6}, {4, 5, 6} Sum of product = (4) + (5) + (6) + (4*5) + (5*6) + (4*6) ... Read More
As the method name suggests copy() method is used to copy the data through various methods available in C++ STL. All the methods differ in functionalities and the parameters. These methods are available in header file. Let’s discuss each method and their functionalities.Copy(start_i1, end_i1, start_i2)This method is used to copy the data from one iterator to another iterator within specified range where both the start and end elements of an iterator are inclusive. It takes three types of arguments i.e. −Start_i1 − It will point to the initial element of the iterator, let’s say, i_1 from where the element ... Read More
There are multiple ways of declaring constants in C and C++. First of all, we need to understand what constant is.What is a Constant?Constant means which can’t be changed. In terms of programming, constants are the fixed values that are assigned to the variables such that they can’t be altered by any other variable or component during the execution of a program. Constants can be of any data type. They are used in the programming for defining the non-changing component of a program. There are some data or variables which have fixed value like Pi have fixed float value as ... Read More
In this article we are going to discuss the vector::upper_bound() and vector::lower_bound() for an array sorted in non-increasing order in C++ STL.Vectors are similar to the dynamic arrays; they have the ability to modify its size itself whenever a value is inserted into or removed from the container where we are storing the value.In a Vector, lower bound returns an iterator pointing to the first element in the range that does not compare the given value. Upper Bound returns an iterator pointing element in the range that smaller than given value.Input 30 30 30 20 20 20 10 10Output Lower bound of ... Read More
In this article we will be discussing the working, syntax and examples of map::empty() function in C++ STL.What is Map in C++ STL?Maps are the associative container, which facilitates to store the elements formed by a combination on key value and mapped value in a specific order. In a map container the data is internally always sorted with the help of its associated keys. The values in map container is accessed by its unique keys.What is map::count()?The map::count( ) is a function which comes under header file. This function counts the elements with specific key, it returns 1 if ... Read More