Friday, November 23, 2012

WRITE A C PROGRAM TO SUM THE SERIES:


/*  WRITE A C PROGRAM TO SUM THE SERIES:

                                               x^2     x^4    x^6
                        COS(x)= 1 - ----- +  ----- - ----- + ...
                                                 2!       4!        6!         
*/

#include<stdio.h>
#include<conio.h>
#include<math.h>

#define PI 3.143

long int fact(int n)  /* To find factorial */
{
            if(n==0)
                        return 1;
            else
                        return(n*fact(n-1));
}
void main()
{
            int i,n,term=1;
            float x,y,sum1,sum2;
            clrscr();
            printf("\n COS Series...");
            printf("\n Enter No. of Terms N [1-12] : ");
            scanf("%ld",&n);
            if(n>=1 && n<=12)
            {
                        printf("\n Enter the Value of 'X' : ");
                        scanf("%f",&y);

                        x=y*(PI/180.00); /* Converting degrees to radians */

                        sum1=1;
                        sum2=0;
                        for(i=2;term<=n;i+=2)
                        {
                                    if(i%4==0)/* To add positive terms */
                                                sum1+=pow(x,i)/(float)fact(i);
                                    else   /* To add Negative terms */
                                                sum2+=pow(x,i)/(float)fact(i);
                                    term++;
                        }
                        printf("\n Cos(%.2f) = %f",y,sum1-sum2);
            }
            else
                        printf("Wrong entry!!!");
            getch();
}


/*         =====OUT PUT=====


      1. COS Series...
             Enter No. of Terms N [1-12] : 9

             Enter the Value of 'X' : 90

             Cos(90.00) = -0.000700
  
      2.   COS Series...
            Enter No. of Terms N [1-12] : 11

            Enter the Value of 'X' : 0

            Cos(0.00) = 1.000000

      3.   COS Series...
             Enter No. of Terms N [1-12] : 0
            Wrong entry!!!

*/

0 comments:

Post a Comment