Wednesday, June 17, 2020

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:

3

Sample Output:

0 2 8

Sample Input:

6

Sample Output:

0 2 8 14 24 34

Solution:

#include<iostream>

using namespace std;

int main()

{

  int i, j, n;

  cin>>n;

  int a[n];

  a[0] = 0;

  a[1] = 2;

  int c = 6;

  for(i = 2; i <= n-1; i+=2){    

    a[i] = a[i-1] + c;

    a[i+1] = a[i] + c;   

    c += 4;

  }

  for(j = 0; j < n; j++){

    cout<<a[j]<<" ";      

  }  

}

No comments:

Post a Comment

horizontal ads