Wednesday, June 17, 2020

Write a program to generate the first 'n' terms of the following series 121, 225, 361,...

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
SAMPLE INPUT:
5
SAMPLE OUTPUT:
121 225 361 529 729
SOLUTION:
#include<iostream>
using namespace std;
int main()
{
  int n;
  cin>>n;
  int a[n];
  a[0] = 121;
  int j = 0,i;
  for(i = 1; i < n; i++){
    a[i] = a[i-1] + 104 + j;
    j += 32;  
  }
    for(i = 0; i < n; i++){
cout<<a[i]<<" ";
  }
}

No comments:

Post a Comment

horizontal ads