Sum of the Series 1 + x/1 + x^2/2 + x^3/3 + .. + x^n/n in C++

sudhir sharma
Updated on 01-Jul-2025 15:33:23

2K+ Views

In this article, we are given a mathematical series. Our task is to write a program to find the sum of the series 1 + x/1 + x^2/2 + x^3/3 + .. + x^n/n. This can also be represented as: $$ 1+\displaystyle\sum\limits_{k=1}^n \left(\frac{x^k}{k}\right) $$ This series without starting 1 is known as the Taylor Expansion Series for -ln(1-x) where ln is the natural log. Example Here is an example of calculating the value of the given series: Input: x = 7, n = 4 Output: 747.08 ... Read More

Sum of the series 1^1 + 2^2 + 3^3 + ... + n^n using recursion in C++

sudhir sharma
Updated on 01-Jul-2025 15:32:28

4K+ Views

In this article, we are given a mathematical series (1^1 + 2^2 + 3^3 + … + n^n) defined by a number n which defines the nth terms of the series. This series can be represented mathematically as: $$ \displaystyle\sum\limits_{k=1}^n k^k $$ The above series does not have any specific mathematical name but is generally referred to as the power tower series. Below is an example of the power tower series up to n. Example The following example calculates the sum of the given series 1^1 + 2^2 + 3^3 + … ... Read More

Count Triplets such that one of the numbers can be written as sum of the other two in C++

Ravi Ranjan
Updated on 01-Jul-2025 15:32:12

1K+ Views

In this article, We are given an array arr[] of integers with length n. Our task is to count the number of triplets such that the sum of any two numbers is equal to the third number. Example Here is an example of counting the triplets whose sum of any two numbers equals the third one: Input: arr[]= {1, 2, 2, 3, 4} Output: 4 The explanation of the above example is as follows: Triplet 1: (1, 2, 3) => 1+2=3 Triplet 2: (1, 2, 3) => 1+2=3 Triplet ... Read More

C++ Program to Implement Sparse Array

Tapas Kumar Ghosh
Updated on 01-Jul-2025 15:30:33

3K+ Views

What is Sparse Array?Sparse array is used to determines the dimension of an array in which most of the elements are zero. It can be used for matrix calculation. Characteristics of Sparse Array Following are list of points that defines the characteristics of a sparse array: Most of the elements are zero or null values which means it is unused. Only non-zero elements and their indexes are stored. It is used to save the memory while comparing to normal array calculation. Example The Sparse array of ... Read More

What are the rules for calling the superclass constructor C++?

Tapas Kumar Ghosh
Updated on 01-Jul-2025 15:09:39

12K+ Views

In C++, a superclass serves as a base/parent class. When we create a derived class (or child class) from a superclass, sometimes we have to call the constructor of the base class along with the constructor of the derived class. Unlike Java, there is no reference variable for the superclass. If the constructor is non-parameterized, then it will be called automatically with the derived class, otherwise, we have to put the superclass constructor in the initializer list of the derived class. Constructor Without Arguments It is a default constructor where no argument is passed to it. Here, we create a ... Read More

What do you mean by a dynamic initialization of variables?

Tapas Kumar Ghosh
Updated on 01-Jul-2025 15:08:26

8K+ Views

Dynamic initialization of object refers to initializing the objects at run time i.e., the initial value of an object is to be provided during runtime. Dynamic initialization can be achieved using constructors and passing parameters values to the constructors. This type of initialization is required to initialize the class variables during runtime. Why do we need the dynamic initialization? Dynamic initialization of objects is useful because It utilizes memory efficiently. Various initialization formats can be provided using overloaded constructors. It has the flexibility of using different formats ... Read More

Read integers from a text file with C++ ifstream

Tapas Kumar Ghosh
Updated on 01-Jul-2025 15:08:03

7K+ Views

In C++, we can read the integer values from text files using the ifstream class that allows the user to perform input operations from the files. Here, we have filename (tp.txt) that used in the program and store some integers value. 21 61 24 05 50 11 12 21 66 55 Example to Read Integers from a Text File In this example, we created one text file to save the integers values and then access those value using file handling operation. #include #include using namespace std; int main() { // Define the maximum ... Read More

How can I get a file's size in C++?

Tapas Kumar Ghosh
Updated on 01-Jul-2025 15:07:34

15K+ Views

In C++, the file size determines the total number of bytes. To get the file size, we can take reference of the fstream header which contains various functions such as in_file(), seekg(), and tellg(). Example Input: The file name is tp.txt content- "Hello Tutorialspoint" Output: Size of the file is 22 bytes Example Input: The file name is testfile.txt content- "Hello Tutorialspoint" Output: File size is 27 bytes Now, we will demonstrate the C++ programs of the above two examples using file handling. Getting File Size in C++ The short form of fstream header is file stream which ... Read More

Program to find longest equivalent sublist after K increments in Python

Niharikaa Aitam
Updated on 30-Jun-2025 16:52:55

219 Views

The longest equivalent sublist after K increments is known as the longest contiguous portion of a list where we can make all the elements equal by performing at most K increment operations. Suppose we have a list of numbers called nums and k. Now, consider an operation where we can increment any one element once. If we can perform operations at most k times, we have to find the longest sub-list containing equal elements. Input Output Scenario Let's consider the following scenario, as we can increment 9 once and 6 four times to get the sublist [10, 10, 10], ... Read More

Program to find longest even value path of a binary tree in Python

Arnab Chakraborty
Updated on 30-Jun-2025 15:49:58

246 Views

A Binary tree is one of the data structures in Python in which each element is known as a Node. Each node should have a minimum of two child nodes. These children nodes are called as left child and the right child. The top node of the binary tree is known as the Root Node, and the nodes with no children are called Leaf Nodes. Following is the representation of the Binary Tree in Python - 10 / \ 5 ... Read More

Advertisements