Given a number N, the task is to find the Nth term of the series where each term differs by 6 and 2 alternately.
Examples:
Input: N = 6
Output: 24
Explanation:
The Nth term is 0 + 6 + 2 + 6 + 2 + 6 + 2 = 24Input: N = 3
Output: 14
Explanation:
The Nth term is 0 + 6 + 2 + 6 = 14
Naive Approach: The idea is to iterate from 1 with an increment of 6 and 2 alternatively, till we reach the Nth term.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find Nth term void findNthTerm(int N) { int ans = 0; // Iterate from 1 till Nth term for (int i = 0; i < N; i++) { // Check if i is even and // then add 6 if (i % 2 == 0) { ans = ans + 6; } // Else add 2 else { ans = ans + 2; } } // Print ans cout << ans << endl; } // Driver Code int main() { int N = 3; findNthTerm(N); return 0; } |
Java
// Java program for the above approach class GFG{ // Function to find Nth term static void findNthTerm(int N) { int ans = 0; // Iterate from 1 till Nth term for (int i = 0; i < N; i++) { // Check if i is even and // then add 6 if (i % 2 == 0) { ans = ans + 6; } // Else add 2 else { ans = ans + 2; } } // Print ans System.out.print(ans +"\n"); } // Driver Code public static void main(String[] args) { int N = 3; findNthTerm(N); } } // This code is contributed by PrinciRaj1992 |
Python3
# Python3 program for the above approach # Function to find Nth term def findNthTerm(N): ans = 0 # Iterate from 1 till Nth term for i in range(N): # Check if i is even and # then add 6 if (i % 2 == 0) : ans = ans + 6 # Else add 2 else : ans = ans + 2 # Print ans print(ans) # Driver Code if __name__=='__main__': N = 3 findNthTerm(N) # This code is contributed by AbhiThakur |
C#
// C# program for the above approach using System; class GFG{ // Function to find Nth term static void findNthTerm(int N) { int ans = 0; // Iterate from 1 till Nth term for (int i = 0; i < N; i++) { // Check if i is even and // then add 6 if (i % 2 == 0) { ans = ans + 6; } // Else add 2 else { ans = ans + 2; } } // Print ans Console.Write(ans +"\n"); } // Driver Code public static void Main() { int N = 3; findNthTerm(N); } } // This code is contributed by AbhiThakur |
14
Time Complexity: O(N)
Efficient Approach: We can find the Nth term by using the below formula:
- If N is Odd: The Nth term is given by (N/2 + 1)*6 + (N/2)*2.
- If N is Even: The Nth term is given by (N/2)*6 + (N/2)*2.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find Nth term void findNthTerm(int N) { int ans; // Check if N is even if (N % 2 == 0) { // Formula for n is even ans = (N / 2) * 6 + (N / 2) * 2; } // Check if N is odd else { // Formula for N is odd ans = (N / 2 + 1) * 6 + (N / 2) * 2; } // Print ans cout << ans << endl; } // Driver Code int main() { int N = 3; findNthTerm(N); return 0; } |
Java
// Java program for the above approach class GFG{ // Function to find Nth term static void findNthTerm(int N) { int ans; // Check if N is even if (N % 2 == 0) { // Formula for n is even ans = (N / 2) * 6 + (N / 2) * 2; } // Check if N is odd else { // Formula for N is odd ans = (N / 2 + 1) * 6 + (N / 2) * 2; } // Print ans System.out.print(ans +"\n"); } // Driver Code public static void main(String[] args) { int N = 3; findNthTerm(N); } } // This code contributed by PrinciRaj1992 |
Python3
# Python3 program for the above approach # Function to find Nth term def findNthTerm(N): ans = 0; # Check if N is even if (N % 2 == 0): # Formula for n is even ans = (N // 2) * 6 + (N // 2) * 2; # Check if N is odd else: # Formula for N is odd ans = (N // 2 + 1) * 6 + (N // 2) * 2; # Print ans print(ans); # Driver Code if __name__ == '__main__': N = 3; findNthTerm(N); # This code is contributed by Rajput-Ji |
C#
// C# program for the above approach using System; class GFG{ // Function to find Nth term static void findNthTerm(int N) { int ans; // Check if N is even if (N % 2 == 0) { // Formula for n is even ans = (N / 2) * 6 + (N / 2) * 2; } // Check if N is odd else { // Formula for N is odd ans = (N / 2 + 1) * 6 + (N / 2) * 2; } // Print ans Console.Write(ans +"\n"); } // Driver Code public static void Main(String[] args) { int N = 3; findNthTerm(N); } } // This code is contributed by PrinciRaj1992 |
14
Time Complexity: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Nth term where K+1th term is product of Kth term with difference of max and min digit of Kth term
- Find the Nth term of the series where each term f[i] = f[i - 1] - f[i - 2]
- Nth term of given recurrence relation having each term equal to the product of previous K terms
- Find the last remaining element after repeated removal of odd and even indexed elements alternately
- Lexicographically smallest string which differs from given strings at exactly K indices
- Nth term of a sequence formed by sum of current term with product of its largest and smallest digit
- Count ways to place all the characters of two given strings alternately
- Program to find Nth term in the given Series
- Program to find the Nth term of the series 3, 7, 13, 21, 31.....
- Program to find Nth term of series 1, 3, 12, 60, 360…
- Program to find the Nth term of series -1, 2, 11, 26, 47......
- Program to find Nth term in the series 0, 0, 2, 1, 4, 2, 6, 3, 8,...
- Program to find Nth term of series 9, 23, 45, 75, 113...
- Program to find Nth term in the series 0, 2, 1, 3, 1, 5, 2, 7, 3,…
- Find Nth term of the series 5, 13, 25, 41, 61...
- Program to find the Nth term of the series 3, 20, 63, 144, 230, ……
- Program to find the Nth term of series 5, 10, 17, 26, 37, 50, 65, 82, ...
- Program to find the Nth term of series 0, 4, 14, 30, 51, 80, 114, 154, 200, ...
- Find Nth term of the series 3, 14, 39, 84...
- Program to find the Nth term of the series 0, 14, 40, 78, 124, ...
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.

