Program for series V in C++

Program to print the given series 121, 225, 361,525,.. in C++,Come up with the logic to understand the series and print the required sets of numbers

Series V

In this post we will be discussing about the series V 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 121, 225, 361,525,..

INPUT & OUTPUT FORMAT:

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

Print the series and refer the sample output for formatting.

SAMPLE INPUT:

4

SAMPLE OUTPUT:

121 225 361 529

Explaination :

By seeing the above output number series we can tell that the number is starting with 121 which is square of 11 and the next number is square of 15, and hence the pattern follows. You can understand it better by seeing the below explaination.

1st number = 11 * 11 = 121

2nd number = 11 + 4 = 15 * 15 = 225

3rd number = 15 + 4 = 19 * 19 = 361

4th number = 19 + 4 = 23 * 23 = 529

Logic to follow to come-up with the solution :

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

2. Assigning value 11 to variable a and value 4 to variable s.

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 square of a.

5. Now add value of a to s and store it in a, and again find the square value of new number 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=11,s=4;

cin>>n;

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

{

cout<<a*a<<" ";

a=a+s;

}

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 !!

Mayank Pathak is the editor and blogger at "The Coding Bro".He loves to codes, learn new technologies, and write the coding solutions and articles for the communities of learners, especiall…