In: Computer Science
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 C 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.
Example output:
Enter number of terms: *value*
*numbers here*
nth term: *value*
PLEASE DO NOT USE IOSTREAM
Program:
#include <stdio.h>
void f(int number) {
int i;
int series;
printf("First n terms in the series : \n");
// Loop iterates from 1 and till the number n
for (i=1; i <= number; i++) {
if (i == 1) { // condition to check for first term
series = 1; // Print first term in the series as 1
} else {
// Other than first term (n-1)^2 + 1
series = ((i-1)*(i-1))+1;
}
// Prints the series of n terms
printf("%d\n", series);
}
// Prints the nth term
printf("nth term %d\n", series);
}
int main()
{
int number;
// Take input for number of terms from user
printf("Enter number of terms: ");
scanf("%d",&number);
// calling function to print series for n numbers
f(number);
return 0;
}
Output: