Find nth term of the series 5 2 13 41
Given a number N, the task is to find the nth term of the series
5, 2, 19, 13, 41, 31, 71, 57….
It is given that value of n can range between 1 and 10000.
Examples:
Input: N = 4 Output:13 Input: N = 15 Output:272
Approach: The problem looks very hard but approach is very simple. If the value of n is given as an odd number, the nth term will be ( ( n + 1 ) ^ 2 ) + n.
Otherwise, it will be ( ( n – 1 ) ^ 2 ) + n.

Implementation:
C++
// C++ program to find nth term of // the series 5 2 13 41 #include<bits/stdc++.h> using namespace std; // function to calculate nth term of the series int nthTermOfTheSeries(int n) { // to store the nth term of series int nthTerm; // if n is even number if (n % 2 == 0) nthTerm = pow(n - 1, 2) + n; // if n is odd number else nthTerm = pow(n + 1, 2) + n; // return nth term return nthTerm; } // Driver code int main() { int n; n = 8; cout << nthTermOfTheSeries(n) << endl; n = 12; cout << nthTermOfTheSeries(n) << endl; n = 102; cout << nthTermOfTheSeries(n) << endl; n = 999; cout << nthTermOfTheSeries(n) << endl; n = 9999; cout << nthTermOfTheSeries(n) << endl; return 0; } // This code is contributed // by Akanksha Rai |
chevron_right
filter_none
C
// C program to find nth term of // the series 5 2 13 41 #include <math.h> #include <stdio.h> // function to calculate nth term of the series int nthTermOfTheSeries(int n) { // to store the nth term of series int nthTerm; // if n is even number if (n % 2 == 0) nthTerm = pow(n - 1, 2) + n; // if n is odd number else nthTerm = pow(n + 1, 2) + n; // return nth term return nthTerm; } // Driver code int main() { int n; n = 8; printf("%d\n", nthTermOfTheSeries(n)); n = 12; printf("%d\n", nthTermOfTheSeries(n)); n = 102; printf("%d\n", nthTermOfTheSeries(n)); n = 999; printf("%d\n", nthTermOfTheSeries(n)); n = 9999; printf("%d\n", nthTermOfTheSeries(n)); return 0; } |
chevron_right
filter_none
Java
// Java program to find nth term of the series 5 2 13 41 import java.lang.Math; class GFG { // function to calculate nth term of the series static long nthTermOfTheSeries(int n) { // to store the nth term of series long nthTerm; // if n is even number if (n % 2 == 0) nthTerm = (long)Math.pow(n - 1, 2) + n; // if n is odd number else nthTerm = (long)Math.pow(n + 1, 2) + n; // return nth term return nthTerm; } // Driver code public static void main(String[] args) { int n; n = 8; System.out.println( nthTermOfTheSeries(n)); n = 12; System.out.println( nthTermOfTheSeries(n)); n = 102; System.out.println( nthTermOfTheSeries(n)); n = 999; System.out.println( nthTermOfTheSeries(n)); n = 9999; System.out.println( nthTermOfTheSeries(n)); //This code is contributed by 29AjayKumar } } |
chevron_right
filter_none
Python3
# Python3 program to find nth term # of the series 5 2 13 41 from math import pow # function to calculate nth term # of the series def nthTermOfTheSeries(n): # to store the nth term of series # if n is even number if (n % 2 == 0): nthTerm = pow(n - 1, 2) + n # if n is odd number else: nthTerm = pow(n + 1, 2) + n # return nth term return nthTerm # Driver code if __name__ == '__main__': n = 8 print(int(nthTermOfTheSeries(n))) n = 12 print(int(nthTermOfTheSeries(n))) n = 102 print(int(nthTermOfTheSeries(n))) n = 999 print(int(nthTermOfTheSeries(n))) n = 9999 print(int(nthTermOfTheSeries(n))) # This code is contributed by # Shashank_Sharma |
chevron_right
filter_none
C#
// C# program to find nth term // of the series 5 2 13 41 using System; class GFG { // function to calculate // nth term of the series static long nthTermOfTheSeries(int n) { // to store the nth term of series long nthTerm; // if n is even number if (n % 2 == 0) nthTerm = (long)Math.Pow(n - 1, 2) + n; // if n is odd number else nthTerm = (long)Math.Pow(n + 1, 2) + n; // return nth term return nthTerm; } // Driver code public static void Main() { int n; n = 8; Console.WriteLine(nthTermOfTheSeries(n)); n = 12; Console.WriteLine( nthTermOfTheSeries(n)); n = 102; Console.WriteLine( nthTermOfTheSeries(n)); n = 999; Console.WriteLine( nthTermOfTheSeries(n)); n = 9999; Console.WriteLine( nthTermOfTheSeries(n)); } } // This code is contributed by Ryuga |
chevron_right
filter_none
PHP
<?php // Php program to find nth term of // the series 5 2 13 41 // function to calculate nth term // of the series function nthTermOfTheSeries($n) { // if n is even number if ($n % 2 == 0) $nthTerm = pow($n - 1, 2) + $n; // if n is odd number else $nthTerm = pow($n + 1, 2) + $n; // return nth term return $nthTerm; } // Driver code $n = 8; echo nthTermOfTheSeries($n) . "\n"; $n = 12; echo nthTermOfTheSeries($n) . "\n"; $n = 102; echo nthTermOfTheSeries($n) . "\n"; $n = 999; echo nthTermOfTheSeries($n) . "\n"; $n = 9999; echo nthTermOfTheSeries($n) . "\n"; // This code is contributed by ita_c ?> |
chevron_right
filter_none
Output:
57 133 10303 1000999 100009999
Recommended Posts:
- Find the Nth term of the series where each term f[i] = f[i - 1] - f[i - 2]
- Find the Nth term of the series 2 + 6 + 13 + 23 + . . .
- Find Nth term of the series 3, 14, 39, 84...
- Find n-th term of series 1, 3, 6, 10, 15, 21...
- Find n-th term of series 3, 9, 21, 41, 71...
- Find the Nth term of the series 14, 28, 20, 40,.....
- Find the Nth term of the series 1 + 2 + 6 + 15 + 31 + 56 + ...
- Find Nth term of the series 1, 8, 54, 384...
- Find n-th term in series 1 2 2 3 3 3 4 4 4 4....
- Find sum of Series with n-th term as n^2 - (n-1)^2
- Find n-th term in the series 7, 15, 32, ...
- Find Nth term of the series 1, 1, 2, 6, 24...
- Find n-th term in the series 9, 33, 73,129 ...
- Find the nth term of the series 0, 8, 64, 216, 512, . . .
- Find nth Term of the Series 1 2 2 4 4 4 4 8 8 8 8 8 8 8 8 ...
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



