﻿ C# - Using Sine and Cosine Series to find TAN Value

# C# - Using Sine and Cosine Series to find TAN Value

I have given here a C# program to find the value of TAN using Sine & cosine Series and without using the library function. This program will print the values got from the series function MyTan and library function tan.

sine series = x - x^3/3! + x^5/5! - x^7 / 7! + x^9 / 9! - .......

cosine series = 1 - x^2/2! + x^4/4! - x^6 / 6! + X^8 / 8! - ....

Tan Series = sine series / cosine series

## Source Code

using System;

using System.Collections.Generic;

using System.Text;

namespace SoftwareAndFinance

{

class Math

{

const double PI = 3.14159265;

// tan series = sine series / cosine series

public static double MyTan(double x)

{

double sineresult = x;

double cosineresult = 1;

double sqx = 0;

double fact = 0;

int index = 0;

int term = 0;

double termfactor = 0;

{

sqx = x * x * x;

fact = 2 * 3;

index = 3;

for (term = 1; term < 20; term++)

{

termfactor = 0;

termfactor = sqx / fact;

if ( (term % 2) == 1)

{

sineresult = sineresult - termfactor;

}

else

{

sineresult = sineresult + termfactor;

}

index++;

fact *= index;

index++;

fact *= index;

sqx *= (x * x);

}

}

{

sqx = x * x;

fact = 2;

index = 2;

for (term = 1; term < 20; term++)

{

termfactor = 0;

termfactor = sqx / fact;

if ( (term % 2) == 1)

{

cosineresult = cosineresult - termfactor;

}

else

{

cosineresult = cosineresult + termfactor;

}

index++;

fact *= index;

index++;

fact *= index;

sqx *= (x * x);

}

}

return sineresult / cosineresult;

}

static void Main(string[] args)

{

for (double d = 0; d <= 360; d += 2.5)

{

Console.WriteLine("{0,6:f2} = {1,8:f5} {2,8:f5}", d, (double)System.Math.Tan(d * PI / 180.0), MyTan(d * PI / 180.0));

}

}

}

}

Click here to get the C# project along with executable

## Output

0.00 =  0.00000  0.00000

2.50 =  0.04366  0.04366

5.00 =  0.08749  0.08749

7.50 =  0.13165  0.13165

10.00 =  0.17633  0.17633

12.50 =  0.22169  0.22169

15.00 =  0.26795  0.26795

17.50 =  0.31530  0.31530

20.00 =  0.36397  0.36397

22.50 =  0.41421  0.41421

25.00 =  0.46631  0.46631

27.50 =  0.52057  0.52057

30.00 =  0.57735  0.57735

32.50 =  0.63707  0.63707

35.00 =  0.70021  0.70021

37.50 =  0.76733  0.76733

40.00 =  0.83910  0.83910

42.50 =  0.91633  0.91633

45.00 =  1.00000  1.00000

47.50 =  1.09131  1.09131

50.00 =  1.19175  1.19175

52.50 =  1.30323  1.30323

55.00 =  1.42815  1.42815

57.50 =  1.56969  1.56969

60.00 =  1.73205  1.73205

62.50 =  1.92098  1.92098

65.00 =  2.14451  2.14451

67.50 =  2.41421  2.41421

70.00 =  2.74748  2.74748

72.50 =  3.17159  3.17159

75.00 =  3.73205  3.73205

77.50 =  4.51071  4.51071

80.00 =  5.67128  5.67128

82.50 =  7.59575  7.59575

85.00 = 11.43005 11.43005

87.50 = 22.90376 22.90376

90.00 = 557135115.02160 557135111.86027

92.50 = -22.90377 -22.90377

95.00 = -11.43005 -11.43005

97.50 = -7.59575 -7.59575

100.00 = -5.67128 -5.67128

102.50 = -4.51071 -4.51071

105.00 = -3.73205 -3.73205

107.50 = -3.17159 -3.17159

110.00 = -2.74748 -2.74748

112.50 = -2.41421 -2.41421

115.00 = -2.14451 -2.14451

117.50 = -1.92098 -1.92098

120.00 = -1.73205 -1.73205

122.50 = -1.56969 -1.56969

125.00 = -1.42815 -1.42815

127.50 = -1.30323 -1.30323

130.00 = -1.19175 -1.19175

132.50 = -1.09131 -1.09131

135.00 = -1.00000 -1.00000

137.50 = -0.91633 -0.91633

140.00 = -0.83910 -0.83910

142.50 = -0.76733 -0.76733

145.00 = -0.70021 -0.70021

147.50 = -0.63707 -0.63707

150.00 = -0.57735 -0.57735

152.50 = -0.52057 -0.52057

155.00 = -0.46631 -0.46631

157.50 = -0.41421 -0.41421

160.00 = -0.36397 -0.36397

162.50 = -0.31530 -0.31530

165.00 = -0.26795 -0.26795

167.50 = -0.22169 -0.22169

170.00 = -0.17633 -0.17633

172.50 = -0.13165 -0.13165

175.00 = -0.08749 -0.08749

177.50 = -0.04366 -0.04366

180.00 =  0.00000  0.00000

182.50 =  0.04366  0.04366

185.00 =  0.08749  0.08749

187.50 =  0.13165  0.13165

190.00 =  0.17633  0.17633

192.50 =  0.22169  0.22169

195.00 =  0.26795  0.26795

197.50 =  0.31530  0.31530

200.00 =  0.36397  0.36397

202.50 =  0.41421  0.41421

205.00 =  0.46631  0.46631

207.50 =  0.52057  0.52057

210.00 =  0.57735  0.57735

212.50 =  0.63707  0.63707

215.00 =  0.70021  0.70021

217.50 =  0.76733  0.76733

220.00 =  0.83910  0.83910

222.50 =  0.91633  0.91633

225.00 =  1.00000  1.00000

227.50 =  1.09131  1.09131

230.00 =  1.19175  1.19175

232.50 =  1.30323  1.30323

235.00 =  1.42815  1.42815

237.50 =  1.56969  1.56969

240.00 =  1.73205  1.73205

242.50 =  1.92098  1.92098

245.00 =  2.14451  2.14451

247.50 =  2.41421  2.41421

250.00 =  2.74748  2.74748

252.50 =  3.17159  3.17159

255.00 =  3.73205  3.73205

257.50 =  4.51071  4.51071

260.00 =  5.67128  5.67128

262.50 =  7.59575  7.59575

265.00 = 11.43005 11.43005

267.50 = 22.90376 22.90376

270.00 = 185711735.63945 185711676.12359

272.50 = -22.90377 -22.90377

275.00 = -11.43005 -11.43005

277.50 = -7.59575 -7.59575

280.00 = -5.67128 -5.67128

282.50 = -4.51071 -4.51071

285.00 = -3.73205 -3.73205

287.50 = -3.17159 -3.17159

290.00 = -2.74748 -2.74748

292.50 = -2.41421 -2.41421

295.00 = -2.14451 -2.14451

297.50 = -1.92098 -1.92098

300.00 = -1.73205 -1.73205

302.50 = -1.56969 -1.56969

305.00 = -1.42815 -1.42815

307.50 = -1.30323 -1.30323

310.00 = -1.19175 -1.19175

312.50 = -1.09131 -1.09131

315.00 = -1.00000 -1.00000

317.50 = -0.91633 -0.91633

320.00 = -0.83910 -0.83910

322.50 = -0.76733 -0.76733

325.00 = -0.70021 -0.70021

327.50 = -0.63707 -0.63707

330.00 = -0.57735 -0.57735

332.50 = -0.52057 -0.52057

335.00 = -0.46631 -0.46631

337.50 = -0.41421 -0.41421

340.00 = -0.36397 -0.36397

342.50 = -0.31530 -0.31530

345.00 = -0.26795 -0.26795

347.50 = -0.22169 -0.22169

350.00 = -0.17633 -0.17633

352.50 = -0.13165 -0.13165

355.00 = -0.08749 -0.08749

357.50 = -0.04366 -0.04366

360.00 =  0.00000  0.00000