Program for Series IV in C++

                           Series IV

In this post we will be discussing about the printing of the given series as in the input.Also the required logic and the coding part will be written in C++.

Series IV– Problem Description

Write a program to generate the following series 0,2,8,14,...,34.

Input format:

The input is an integer which denotes 'n'.

Output format:

Print the series and refer the sample output for formatting.

Sample Input:

6

Sample Output:

0 2 8 14 24 34

Logic to follow to come-up with the solution :

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

2. Take the input from the user till the series have to print.

3. Apply and iterate the loop from initialization as 1 till the number of elements inputted.

4. Find the square values of each number till the loop continues and store it in variable b.

5. Now subtract 2 from the squares value if the number is even and print it.

6. Similarly subtract 1 from the squares value if the number is odd and print it.

7. At last we will find the required sets of numbers in the series.

Let’s write the required code for the problem :

Code :

#include<iostream>

using namespace std;

int main()

{

 int n,b;

 cin>>n;

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

  {

    b=i*i;

    if(b%2==0)

     cout<<b-2<<" ";

    else

     cout<<b-1<<" ";

  }

  return 0;

}

Sample Test Cases have been passed successfully

You can run same set of code at this portal, click here to go to the online portal and just give required set of inputs and click on Execute to run the program.

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

Best Buy Products