Software & Finance





C# - Using Sine Series to find SIN Value





I have given here a C# program to find the value of sin using Sine Series and without using the library function. This program will print the values got from the series function MySin and System.Math.Sin.

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

 

Source Code


using System;

using System.Collections.Generic;

using System.Text;

 

namespace SoftwareAndFinance

{

    class Math

    {

        const double PI = 3.14159265;

        // sin series = x - x^3/3! + x^5/5! - x^7 / 7!

        public static double MySin(double x)

        {

            double sqx = x * x * x;

 

            double sineresult = x;

            double fact = 2 * 3;

            int index = 3;

            int term = 0;

            double termfactor = 0;

 

            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);

            }

            return sineresult;

        }

        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.Sin(d * PI / 180.0), MySin(d * PI / 180.0));

            }

 

        }

    }

}

Click here to get the C# project along with executable

 

Output


 

  0.00 =  0.00000  0.00000

  2.50 =  0.04362  0.04362

  5.00 =  0.08716  0.08716

  7.50 =  0.13053  0.13053

 10.00 =  0.17365  0.17365

 12.50 =  0.21644  0.21644

 15.00 =  0.25882  0.25882

 17.50 =  0.30071  0.30071

 20.00 =  0.34202  0.34202

 22.50 =  0.38268  0.38268

 25.00 =  0.42262  0.42262

 27.50 =  0.46175  0.46175

 30.00 =  0.50000  0.50000

 32.50 =  0.53730  0.53730

 35.00 =  0.57358  0.57358

 37.50 =  0.60876  0.60876

 40.00 =  0.64279  0.64279

 42.50 =  0.67559  0.67559

 45.00 =  0.70711  0.70711

 47.50 =  0.73728  0.73728

 50.00 =  0.76604  0.76604

 52.50 =  0.79335  0.79335

 55.00 =  0.81915  0.81915

 57.50 =  0.84339  0.84339

 60.00 =  0.86603  0.86603

 62.50 =  0.88701  0.88701

 65.00 =  0.90631  0.90631

 67.50 =  0.92388  0.92388

 70.00 =  0.93969  0.93969

 72.50 =  0.95372  0.95372

 75.00 =  0.96593  0.96593

 77.50 =  0.97630  0.97630

 80.00 =  0.98481  0.98481

 82.50 =  0.99144  0.99144

 85.00 =  0.99619  0.99619

 87.50 =  0.99905  0.99905

 90.00 =  1.00000  1.00000

 92.50 =  0.99905  0.99905

 95.00 =  0.99619  0.99619

 97.50 =  0.99144  0.99144

100.00 =  0.98481  0.98481

102.50 =  0.97630  0.97630

105.00 =  0.96593  0.96593

107.50 =  0.95372  0.95372

110.00 =  0.93969  0.93969

112.50 =  0.92388  0.92388

115.00 =  0.90631  0.90631

117.50 =  0.88701  0.88701

120.00 =  0.86603  0.86603

122.50 =  0.84339  0.84339

125.00 =  0.81915  0.81915

127.50 =  0.79335  0.79335

130.00 =  0.76604  0.76604

132.50 =  0.73728  0.73728

135.00 =  0.70711  0.70711

137.50 =  0.67559  0.67559

140.00 =  0.64279  0.64279

142.50 =  0.60876  0.60876

145.00 =  0.57358  0.57358

147.50 =  0.53730  0.53730

150.00 =  0.50000  0.50000

152.50 =  0.46175  0.46175

155.00 =  0.42262  0.42262

157.50 =  0.38268  0.38268

160.00 =  0.34202  0.34202

162.50 =  0.30071  0.30071

165.00 =  0.25882  0.25882

167.50 =  0.21644  0.21644

170.00 =  0.17365  0.17365

172.50 =  0.13053  0.13053

175.00 =  0.08716  0.08716

177.50 =  0.04362  0.04362

180.00 =  0.00000  0.00000

182.50 = -0.04362 -0.04362

185.00 = -0.08716 -0.08716

187.50 = -0.13053 -0.13053

190.00 = -0.17365 -0.17365

192.50 = -0.21644 -0.21644

195.00 = -0.25882 -0.25882

197.50 = -0.30071 -0.30071

200.00 = -0.34202 -0.34202

202.50 = -0.38268 -0.38268

205.00 = -0.42262 -0.42262

207.50 = -0.46175 -0.46175

210.00 = -0.50000 -0.50000

212.50 = -0.53730 -0.53730

215.00 = -0.57358 -0.57358

217.50 = -0.60876 -0.60876

220.00 = -0.64279 -0.64279

222.50 = -0.67559 -0.67559

225.00 = -0.70711 -0.70711

227.50 = -0.73728 -0.73728

230.00 = -0.76604 -0.76604

232.50 = -0.79335 -0.79335

235.00 = -0.81915 -0.81915

237.50 = -0.84339 -0.84339

240.00 = -0.86603 -0.86603

242.50 = -0.88701 -0.88701

245.00 = -0.90631 -0.90631

247.50 = -0.92388 -0.92388

250.00 = -0.93969 -0.93969

252.50 = -0.95372 -0.95372

255.00 = -0.96593 -0.96593

257.50 = -0.97630 -0.97630

260.00 = -0.98481 -0.98481

262.50 = -0.99144 -0.99144

265.00 = -0.99619 -0.99619

267.50 = -0.99905 -0.99905

270.00 = -1.00000 -1.00000

272.50 = -0.99905 -0.99905

275.00 = -0.99619 -0.99619

277.50 = -0.99144 -0.99144

280.00 = -0.98481 -0.98481

282.50 = -0.97630 -0.97630

285.00 = -0.96593 -0.96593

287.50 = -0.95372 -0.95372

290.00 = -0.93969 -0.93969

292.50 = -0.92388 -0.92388

295.00 = -0.90631 -0.90631

297.50 = -0.88701 -0.88701

300.00 = -0.86603 -0.86603

302.50 = -0.84339 -0.84339

305.00 = -0.81915 -0.81915

307.50 = -0.79335 -0.79335

310.00 = -0.76604 -0.76604

312.50 = -0.73728 -0.73728

315.00 = -0.70711 -0.70711

317.50 = -0.67559 -0.67559

320.00 = -0.64279 -0.64279

322.50 = -0.60876 -0.60876

325.00 = -0.57358 -0.57358

327.50 = -0.53730 -0.53730

330.00 = -0.50000 -0.50000

332.50 = -0.46175 -0.46175

335.00 = -0.42262 -0.42262

337.50 = -0.38268 -0.38268

340.00 = -0.34202 -0.34202

342.50 = -0.30071 -0.30071

345.00 = -0.25882 -0.25882

347.50 = -0.21644 -0.21644

350.00 = -0.17365 -0.17365

352.50 = -0.13053 -0.13053

355.00 = -0.08716 -0.08716

357.50 = -0.04362 -0.04362

360.00 =  0.00000  0.00000