Tuesday, June 16, 2020

Write a program to generate the first 'n' terms of the following series 0.5, 1.5, 4.5, 13.5, ...

Write a program to generate the first 'n' terms of the following series 0.5, 1.5, 4.5, 13.5, ...

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:

5

Sample Output:

0.5 1.5 4.5 13.5 40.5

Sample Input 2:

6

Sample Output:

0.5 1.5 4.5 13.5 40.5 121.5

Solution

#include<iostream>
using namespace std;
int main(){
  int n; 
  cin>>n;
   float a[n];
  a[0] = 0.5;
  int j = 1;
  for(int i = 1; i < n; i++){
a[i] = a[i-1] + j;
    j *= 3;
  }
  for(int i = 0; i < n; i++ ){
  cout<<a[i]<<" ";
  }
return 0;
}

No comments:

Post a Comment

horizontal ads