Program for Series III in C++

Series III

In this post we will be discussing about the series III problem, how to come up with the logic to understand the series and print the required sets of numbers.Also, the required logic is discussed and the coding part is written in C++.

Problem Description

Write a program to generate the first 'n' terms of the following series 6, 11, 21, 36, 56,...

Input Format:

The input is an integer 'n' which denotes the number of terms to be printed in the series.

Output Format:

Print the series and refer the sample output for formatting.

Sample Input:

6

Sample Output:

6 11 21 36 56 81

Explaination :

By seeing the above output number series we can tell that the number is starting with 6 and each time the multiplication of 5 is added with the next number. You can understand it better by seeing the below explaination.

1st number = 6

2nd number = 6 + 5 * 1= 11

3rd number = 11 + 5 * 2 = 21

4th number = 21 + 5 * 3 = 36

5th number = 36 + 5 * 4 = 56

6th number = 56 + 5 * 5 = 81

Logic to follow to come-up with the solution :

1.Declare the required sets of variables to use in the code.

2. Assigning value 6 to variable a and value 5 to variable x.

3. Input the user declared variable and apply the loop.

4. Iterate it from 1 till the value of input number and print the value of a.

5. Now add value of a to x and store it in a, similarly increment the value of x with 5 each time the loop iterates.

6. Hence finally we get the required set of series as output.

Let’s write the required code for the problem :

Code :

#include<iostream>

using namespace std;

int main()

{

  int n,a=6,x=5;

  cin>>n;

 for(int i=1;i<=n;i++)

 {

    cout<<a<<" ";

    a+=x;

    x+=5;

 }

 return 0;

}

Sample Test Cases have been passed successfully

If you liked this post, then share this with your friends and if anything is confusing or incorrect then let me know in the comment section.

Thanks from my side, this is Mayank, keep learning and exploring !!

Next Post Previous Post
No Comment
Add Comment
comment url