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++.
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.
121 225 361 529
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 :
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 !!