Tuesday, June 16, 2020

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

Input Format:

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

Output Format:

Print the series and refer the sample output for formatting.

Sample Input 1:

4

Sample Output:

121 225 361 529

Sample Input 2:

5

Sample Output:

121 225 361 529 729

Solution:

#include<iostream> using namespace std; int main(){ int i, n, j = 0; cin>>n; int a[n]; a[0] = 121; for(i = 1; i < n; i++){ a[i] = a[i-1] + 104 + j; j += 32; } for(i = 0; i < n; i++){ cout<<a[i]<<" "; } return 0; }

No comments:

Post a Comment

horizontal ads