Software & Finance





C Programming (Turbo / Visual C++ Compiler) - Using Cosine Series to find COS Value





I have given here a C program to find the value of cos using Cosine Series and without using the library function. This program will print the values got from the series function MyCos and library function cos.

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



Source Code


#include <math.h>

#include <stdio.h>

 

const double PI = 3.14159265; 

// cos series = 1 - x^2/2! + x^4/4! - x^6 / 6!

double MyCos(double x)

{

    double sqx = x * x; 

    double cosineresult = 1;

    double fact = 2;

    int index = 2;

    int term = 0;

    double termfactor = 0;

 

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

    {

        termfactor = 0;

        termfactor = sqx / fact;

        if(term %2)

        {

            cosineresult = cosineresult - termfactor;

        }

        else

        {

            cosineresult = cosineresult + termfactor;

        }

        index++;

        fact *= index;

        index++;

        fact *= index;

        sqx *= (x*x);

    }

    return cosineresult;

} 

 

int main()

{

    double i = 0;

    for(i = 0; i <= 360; i += 4.75)

    {

        printf("%.4lf = %.4lf\t%.4lf\n", i, cos(i * PI / 180), MyCos(i * PI / 180));

        //printf("%.4lf\n", cos(i * PI / 180) - MyCos(i * PI / 180));

    }

    return 0;

}

Output


0.0000  = 1.0000            1.0000

4.7500  = 0.9966            0.9966

9.5000  = 0.9863            0.9863

14.2500 = 0.9692        0.9692

19.0000 = 0.9455        0.9455

23.7500 = 0.9153        0.9153

28.5000 = 0.8788        0.8788

33.2500 = 0.8363        0.8363

38.0000 = 0.7880        0.7880

42.7500 = 0.7343        0.7343

47.5000 = 0.6756        0.6756

52.2500 = 0.6122        0.6122

57.0000 = 0.5446        0.5446

61.7500 = 0.4733        0.4733

66.5000 = 0.3987        0.3987

71.2500 = 0.3214        0.3214

76.0000 = 0.2419        0.2419

80.7500 = 0.1607        0.1607

85.5000 = 0.0785        0.0785

90.2500 = -0.0044       -0.0044

95.0000 = -0.0872       -0.0872

99.7500 = -0.1693       -0.1693

104.5000 = -0.2504      -0.2504

109.2500 = -0.3297      -0.3297

114.0000 = -0.4067      -0.4067

118.7500 = -0.4810      -0.4810

123.5000 = -0.5519      -0.5519

128.2500 = -0.6191      -0.6191

133.0000 = -0.6820      -0.6820

137.7500 = -0.7402      -0.7402

142.5000 = -0.7934      -0.7934

147.2500 = -0.8410      -0.8410

152.0000 = -0.8829      -0.8829

156.7500 = -0.9188      -0.9188

161.5000 = -0.9483      -0.9483

166.2500 = -0.9713      -0.9713

171.0000 = -0.9877      -0.9877

175.7500 = -0.9973      -0.9973

180.5000 = -1.0000      -1.0000

185.2500 = -0.9958      -0.9958

190.0000 = -0.9848      -0.9848

194.7500 = -0.9670      -0.9670

199.5000 = -0.9426      -0.9426

204.2500 = -0.9118      -0.9118

209.0000 = -0.8746      -0.8746

213.7500 = -0.8315      -0.8315

218.5000 = -0.7826      -0.7826

223.2500 = -0.7284      -0.7284

228.0000 = -0.6691      -0.6691

232.7500 = -0.6053      -0.6053

237.5000 = -0.5373      -0.5373

242.2500 = -0.4656      -0.4656

247.0000 = -0.3907      -0.3907

251.7500 = -0.3132      -0.3132

256.5000 = -0.2334      -0.2334

261.2500 = -0.1521      -0.1521

266.0000 = -0.0698      -0.0698

270.7500 = 0.0131       0.0131

275.5000 = 0.0958       0.0958

280.2500 = 0.1779       0.1779

285.0000 = 0.2588       0.2588

289.7500 = 0.3379       0.3379

294.5000 = 0.4147       0.4147

299.2500 = 0.4886       0.4886

304.0000 = 0.5592       0.5592

308.7500 = 0.6259       0.6259

313.5000 = 0.6884       0.6884

318.2500 = 0.7461       0.7461

323.0000 = 0.7986       0.7986

327.7500 = 0.8457       0.8457

332.5000 = 0.8870       0.8870

337.2500 = 0.9222       0.9222

342.0000 = 0.9511       0.9511

346.7500 = 0.9734       0.9734

351.5000 = 0.9890       0.9890

356.2500 = 0.9979       0.9979

Press any key to continue . . .