Given a value of tan(Θ) and a variable n <=15. The task is to find the value of tan(nΘ) using property of trignometric functions.
Examples:
Input: tan(Θ) = 0.3, n = 10 Output: -2.15283 Input: tan(Θ) = 0.3, n = 5 Output: 0.37293
This problem can be solved using De moivre’s theoremand Binomial theorem

Below is the implementation of above approach:
C++
// C++ program to find the value // of cos(n-theta) #include <bits/stdc++.h> #define ll long long int #define MAX 16 using namespace std; ll nCr[MAX][MAX] = { 0 }; // This function use to calculate the // binomial cofficient upto 15 void binomial() { // use simple DP to find cofficient for (int i = 0; i < MAX; i++) { for (int j = 0; j <= i; j++) { if (j == 0 || j == i) nCr[i][j] = 1; else nCr[i][j] = nCr[i - 1][j] + nCr[i - 1][j - 1]; } } } // Function to find the value of double findTanNTheta(double tanTheta, ll n) { // store required answer double ans = 0, numerator = 0, denominator = 0; // use to toggle sign in sequence. ll toggle = 1; // calculate numerator for (int i = 1; i <= n; i += 2) { numerator = numerator + nCr[n][i] * pow(tanTheta, i) * toggle; toggle = toggle * -1; } // calculate denominator denominator = 1; toggle = -1; for (int i = 2; i <= n; i += 2) { numerator = numerator + nCr[n][i] * pow(tanTheta, i) * toggle; toggle = toggle * -1; } ans = numerator / denominator; return ans; } // Driver code. int main() { binomial(); double tanTheta = 0.3; ll n = 10; cout << findTanNTheta(tanTheta, n) << endl; return 0; } |
Java
// Java program to find the value // of cos(n-theta) public class GFG { private static final int MAX = 16 ; static long nCr[][] = new long [MAX][MAX] ; // This function use to calculate the // binomial coefficient upto 15 static void binomial() { // use simple DP to find cofficient for (int i = 0; i < MAX; i++) { for (int j = 0; j <= i; j++) { if (j == 0 || j == i) nCr[i][j] = 1; else nCr[i][j] = nCr[i - 1][j] + nCr[i - 1][j - 1]; } } } // Function to find the value of static double findTanNTheta(double tanTheta, int n) { // store required answer double ans = 0, numerator = 0, denominator = 0; // use to toggle sign in sequence. long toggle = 1; // calculate numerator for (int i = 1; i <= n; i += 2) { numerator = numerator + nCr[n][i] * Math.pow(tanTheta, i) * toggle; toggle = toggle * -1; } // calculate denominator denominator = 1; toggle = -1; for (int i = 2; i <= n; i += 2) { numerator = numerator + nCr[n][i] * Math.pow(tanTheta, i) * toggle; toggle = toggle * -1; } ans = numerator / denominator; return ans; } // Driver code public static void main(String args[]) { binomial(); double tanTheta = 0.3; int n = 10; System.out.println(findTanNTheta(tanTheta, n)); } // This code is contributed by ANKITRAI1 } |
Python3
# Python3 program to find the value # of cos(n-theta) import math MAX=16nCr=[[0 for i in range(MAX)] for i in range(MAX)] # Function to calculate the binomial # cofficient upto 15 def binomial(): # use simple DP to find cofficient for i in range(MAX): for j in range(0,i+1): if j == 0 or j == i: nCr[i][j] = 1 else: nCr[i][j] = nCr[i - 1][j] + nCr[i - 1][j - 1] # Function to find the value of cos(n-theta) def findTanNTheta(tanTheta,n): # store required answer numerator=0 denominator=1 # to store required answer ans = 0 # use to toggle sign in sequence. toggle = 1 # calculate numerator for i in range(1,n+1,2): numerator = (numerator + nCr[n][i]* (tanTheta**(i)) * toggle) toggle = toggle * -1 # calculate denominator toggle=-1 for i in range(2,n+1,2): numerator = (numerator + nCr[n][i]* (tanTheta**i) * toggle) toggle = toggle * -1 ans=numerator/denominator return ans # Driver code if __name__=='__main__': binomial() tanTheta = 0.3 n = 10 print(findTanNTheta(tanTheta, n)) # this code is contributed by sahilshelangia |
C#
// C# program to find the value // of cos(n-theta) using System; public class GFG { private static int MAX = 16 ; static long[,] nCr = new long [MAX,MAX] ; // This function use to calculate the // binomial coefficient upto 15 static void binomial() { // use simple DP to find cofficient for (int i = 0; i < MAX; i++) { for (int j = 0; j <= i; j++) { if (j == 0 || j == i) nCr[i,j] = 1; else nCr[i,j] = nCr[i - 1,j] + nCr[i - 1,j - 1]; } } } // Function to find the value of static double findTanNTheta(double tanTheta, int n) { // store required answer double ans = 0, numerator = 0, denominator = 0; // use to toggle sign in sequence. long toggle = 1; // calculate numerator for (int i = 1; i <= n; i += 2) { numerator = numerator + nCr[n,i] * Math.Pow(tanTheta, i) * toggle; toggle = toggle * -1; } // calculate denominator denominator = 1; toggle = -1; for (int i = 2; i <= n; i += 2) { numerator = numerator + nCr[n,i] * Math.Pow(tanTheta, i) * toggle; toggle = toggle * -1; } ans = numerator / denominator; return ans; } // Driver code public static void Main() { binomial(); double tanTheta = 0.3; int n = 10; Console.Write(findTanNTheta(tanTheta, n)); } } |
PHP
<?php // PHP program to find the value // of cos(n-theta) $MAX=16; $nCr=array_fill(0, $MAX, array_fill(0, $MAX, 0)); // This function use to calculate the // binomial cofficient upto 15 function binomial() { global $MAX,$nCr; // use simple DP to find cofficient for ($i = 0; $i < $MAX; $i++) { for ($j = 0; $j <= $i; $j++) { if ($j == 0 || $j == $i) $nCr[$i][$j] = 1; else $nCr[$i][$j] = $nCr[$i - 1][$j] + $nCr[$i - 1][$j - 1]; } } } // Function to find the value of function findTanNTheta($tanTheta, $n) { global $MAX,$nCr; // store required answer $ans = 0; $numerator = 0; $denominator = 0; // use to toggle sign in sequence. $toggle = 1; // calculate numerator for ($i = 1; $i <= $n; $i += 2) { $numerator = $numerator + $nCr[$n][$i] * pow($tanTheta, $i) * $toggle; $toggle = $toggle * -1; } // calculate denominator $denominator = 1; $toggle = -1; for ($i = 2; $i <= $n; $i += 2) { $numerator = $numerator + $nCr[$n][$i] * pow($tanTheta, $i) * $toggle; $toggle = $toggle * -1; } $ans = $numerator / $denominator; return $ans; } // Driver code. binomial(); $tanTheta = 0.3; $n = 10; echo findTanNTheta($tanTheta, $n); // This code is contributed by mits ?> |
-2.15283
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:
- Find the maximum possible value of the minimum value of modified array
- Program to find greater value between a^n and b^n
- Program to Find the value of cos(nΘ)
- Program to find the value of sin(nΘ)
- Program to find the Depreciation of Value
- Program to find value of 1^k + 2^k + 3^k + ... + n^k
- Program to find absolute value of a given number
- C program to calculate the value of nPr
- Program to calculate the value of sin(x) and cos(x) using Expansion
- Program to print ASCII Value of a character
- Program to calculate value of nCr
- Program to calculate the value of nCr Efficiently
- Find length of period in decimal value of 1/n
- Find minimum number of coins that make a given value
- Find the minimum value of m that satisfies ax + by = m and all values after m also satisfy
- Find minimum value to assign all array elements so that array product becomes greater
- Find minimum value of y for the given x values in Q queries from all the given set of lines
- Find value of (1^n + 2^n + 3^n + 4^n ) mod 5
- Find value of y mod (2 raised to power x)
- Find value of (n^1 + n^2 + n^3 + n^4) mod 5 for given n
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.

