Write a program to display the following series upto n terms:
21, 32, 43......................
Answers
Answer:
Input : 4
Output : 3
Input : 11
Output : 32
Recommended: Please try your approach on {IDE} first, before moving on to the solution.
On observing carefully, you will find that the series is a mixture of 2 series:
All the odd terms in this series form a geometric series.
All the even terms form yet another geometric series.
The approach to solving the problem is quite simple. The odd positioned terms in the given series form a GP series with first term = 1 and common ration = 2. Similarly, the even positioned terms in the given series form a GP series with first term = 1 and common ration = 3.
Therefore first check whether the input number N is even or odd. If it is even, set N=N/2(since there are Two GP series running parallely) and find the Nth term by using formula an = a1·rn-1 with r=3.
Similarly, if N is odd, set N=(n/2)+1 and do the same as previous with r=2.
Below is the implementation of above approach:
hope it helps
Answer:
// C++ program to find Nth term
// in the given Series
#include <iostream>
#include <math.h>
using namespace std;
// Function to find the nth term
// in the given series
void findNthTerm(int n)
{
// If input number is even
if (n % 2 == 0) {
n = n / 2;
cout << pow(3, n - 1) << endl;
}
// If input number is odd
else {
n = (n / 2) + 1;
cout << pow(2, n - 1) << endl;
}
}
// Driver Code
int main()
{
int N = 4;
findNthTerm(N);
N = 11;
findNthTerm(N);
return 0;
}