C++ program to find n-th term of series 1, 3, 6, 10, 15, 21…

C++Server Side ProgrammingProgramming

In this problem, we are given an integer N. The task is to find the n-th term in series 1, 3, 6, 10, 15, 21, 28....

Let’s take an example to understand the problem,

Input

N = 7

Output

28

Explanation

The series is 1, 3, 6, 10, 15, 21, 28...

Solution Approach

A simple solution to the problem is by finding the general term of the series. On observing the series we can see that the ith number of the series is the sum of (i-1)th term and i.

This type of number is called triangular number.

To solve the problem, we will loop till n, and for each iteration add the current index with the last element’s value. At last return the Nth elements value.

Program to illustrate the working of our solution,

Example

 Live Demo

#include <iostream>
using namespace std;
int findNthTerm(int N) {
   int NthTerm = 0;
   for (int i = 1; i <= N; i++)
      NthTerm = NthTerm + i;
   return NthTerm;
}
int main() {
   int N = 8;
   cout<<"The "<<N<<"th term of the series is "<<findNthTerm(N);
   return 0;
}

Output

The 8th term of the series is 36
raja
Published on 13-Mar-2021 16:51:09
Advertisements