Sum of the Tan ( x ) expansion upto N terms | CPP program to find tan ( x ) expansion ; Function to find factorial of a number ; To store factorial of a number ; Return the factorial of a number ; Function to find tan ( x ) upto n terms ; To store value of the expansion ; This loops here calculate Bernoulli number which is further used to get the coefficient in the expansion of tan x ; Print the value of expansion ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int fac ( int num ) { if ( num == 0 ) return 1 ; int fact = 1 ; for ( int i = 1 ; i <= num ; i ++ ) fact = fact * i ; return fact ; } void Tanx_expansion ( int terms , int x ) { double sum = 0 ; for ( int i = 1 ; i <= terms ; i += 1 ) { double B = 0 ; int Bn = 2 * i ; for ( int k = 0 ; k <= Bn ; k ++ ) { double temp = 0 ; for ( int r = 0 ; r <= k ; r ++ ) temp = temp + pow ( -1 , r ) * fac ( k ) * pow ( r , Bn ) / ( fac ( r ) * fac ( k - r ) ) ; B = B + temp / ( ( double ) ( k + 1 ) ) ; } sum = sum + pow ( -4 , i ) * ( 1 - pow ( 4 , i ) ) * B * pow ( x , 2 * i - 1 ) / fac ( 2 * i ) ; } cout << setprecision ( 10 ) << sum ; } int main ( ) { int n = 6 , x = 1 ; Tanx_expansion ( n , x ) ; return 0 ; }