158k views
2 votes
Given the following series: 1, 2, 5, 26, 677, ….. such that the nth term of the series equals to (n-1)th ^2 +1 and the first term of the series is 1. Write a program using recursive function named f to compute the nth term. Use for loop to print the values of first n terms in the series. You will take input n from the user.

User MatHatrik
by
5.5k points

1 Answer

5 votes

Answer:

// program in C++.

#include <bits/stdc++.h>

using namespace std;

// function to fint the nth term of the series

long long int f(int n) {

// base case

if (n <= 1)

// return

return 1;

else

// recursive call

return pow(f(n - 1), 2) + 1;

}

// main function

int main()

{

// variable

int term;

cout<<"Enter the number of terms:";

// read the number of term of series

cin>>term;

// print the series

for(int i=1;i<=term;i++)

{

// call the function

cout<<f(i)<<" ";

}

return 0;

}

Step-by-step explanation:

Read the number of terms from user and assign it to variable "term".Call the function f() to find the term of the series.Find the nth term of the series as ""(n-1)th ^2 +1" with function f() and print it within the for loop.

Output:

Enter the number of terms:6

1 2 5 26 677 458330

User Yacovm
by
6.0k points