Showing posts with label series. Show all posts
Showing posts with label series. Show all posts

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]<<" ";
  }
}

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]<<" ";      

  }  

}

Write a program to generate the first 'n' terms of the following series 6, 11, 21, 36, 56,...

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:

6

Sample Output:

6 11 21 36 56 81

Sample Input 2:

10

Sample Output:

6 11 21 36 56 81 111 146 186 231

Solution:

#include<iostream>

using namespace std;

int main(){

  int n,i,j = 1;

  cin>>n;

  int a[n];

  a[0] = 6;

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

    a[i] = a[i-1] + 5 * j;

    j += 1;

  }

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

cout<<a[i]<<" ";

  }

  return 0;

}

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; }

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;
}

horizontal ads