Nth term of a Custom Fibonacci series | C ++ implementation of the Custom Fibonacci series ; Function to return the nth term of the required sequence ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int nth_term ( int a , int b , int n ) { int z = 0 ; if ( n % 6 == 1 ) z = a ; else if ( n % 6 == 2 ) z = b ; else if ( n % 6 == 3 ) z = b - a ; else if ( n % 6 == 4 ) z = - a ; else if ( n % 6 == 5 ) z = - b ; if ( n % 6 == 0 ) z = - ( b - a ) ; return z ; } int main ( ) { int a = 10 , b = 17 , n = 3 ; cout << nth_term ( a , b , n ) ; return 0 ; }